Communication between Code Behind and Viewmodel Xamarin

Asked

Viewed 203 times

0

I have two pages created on Xamarin.forms, on the first page I have a list and on the second I have the details of each item selected previously. I send the selected item from the first page to the second through the communication of the two code Behind.

Page where I have the list(Code Behind):

private async void ListViewCats_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var SelectedCat = e.SelectedItem as Models.Cat;
            if (SelectedCat != null)
            {
                await Navigation.PushAsync(new Views.DetailsPage(SelectedCat));
                ListViewCats.SelectedItem = null;
            }
        }

Details page(Code Behind):

Cat SelectedCat;
public DetailsPage(Cat selectedCat)
{
    this.SelectedCat = selectedCat;
}

This way I can display the data of this object normally in xaml. But I want to pass on that value selectedCat to the ViewModel of details I created, how can I do this?

2 answers

1

I run this operation in a simpler way. You can use the plugin Settingsplugin (also found in nuget packages in Visual Studio) and simply "set" the value in a variable and then rescue it from wherever you want.

Creation of the variable:

private const string UserNameKey = "username_key";
private static readonly string UserNameDefault = string.Empty;

public static string UserName
{
  get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
  set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}

In your code-Behind where the List is, when there is the click, it is enough:

Classe.UserName = "seu item clicado";

In your other Viewmodel or other code-behide, just receive the value.

var suaVariavel = Classe.UserName;

0


I went through a similar situation, I had 4 pages that together filled a same entity (of orders), the solution I used at the time was to save the data of each page in a txt and go rescuing on the next page. It is quite simple to do, you just need to take care to delete the file when no longer using it.

Of course there are other means (such as this), It all depends on how you want to do it.

If you want to try, you will use Newtonsoft.json and System.io.

To save text:

public void SaveText(string filename, string text)
    {
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = System.IO.Path.Combine(documentsPath, filename);
        System.IO.File.WriteAllText(filePath, text);
    }

To load the text:

public string LoadText(string filename)
    {
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = System.IO.Path.Combine(documentsPath, filename);
        if (System.IO.File.Exists(filePath))
            return System.IO.File.ReadAllText(filePath);
        else return string.Empty;
    }

Methods of the viewmodel:

    private void SalvarTxtPedido()
    {
        string ped = JsonConvert.SerializeObject(_Ped);
        _SaL.SaveText("Pedido.txt", ped);
    }


public bool CarregaArquivoPedido()
    {
        if (_SaL.ValidateExist("Pedido.txt"))
        {
            _Ped.CardCode = SelectedParceiro.Value.CardCode;
            _Ped.CardName = SelectedParceiro.Value.CardName;

            string jsonPedido = _SaL.LoadText("Pedido.txt");
            _Ped = JsonConvert.DeserializeObject<Ped>(jsonPedido);
            return true;
        }
        else
            return false;
    }
  • Thank you very much, I will analyze the Link I find it difficult to create a txt to make a small manipulation

  • Blz, if useful, mark as answer. Your question may be the same as others.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.