Communication between Code Behind and ViewModel Xamarin

0

I have two pages created in Xamarin.forms , on the first page I have a list and on the second 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;
            }
        }

Code Behind page:

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

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

    
asked by anonymous 06.04.2017 / 04:46

2 answers

0

I went through a similar situation, I had 4 pages that together filled the same entity (of requests), the solution I used at the time was to save the data of each page in a txt and go rescue on the next page. It's very simple to do, you just have to be careful to delete the file when you're not using it anymore.

Of course there are other ways (such as this one ), 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 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;
    }

Viewmodel methods:

    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;
    }
    
06.04.2017 / 13:52
1

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

Creating 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 located, when there is a click, just:

Classe.UserName = "seu item clicado";

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

var suaVariavel = Classe.UserName;
    
14.06.2017 / 14:17