I have datagridview in my popup page, select items from there then i want to update selected value on that main page. But i cant update that label. I bind this 2 pages the same viewmodel. I control popup page close with this:
protected override void OnDisappearing()
{
viewModel.CarilisteClose = true;
base.OnDisappearing();
}
This is my property:
public bool CarilisteClose { get => carilisteClose; set {
carilisteClose = value;
OnPropertyChanged();
Console.WriteLine(carilisteClose);
changeCariText(carilisteClose);
} }
After that i can see from output that property True and selected item. But SearchCari cant update after pupoppage closing.
public string SearchCari => $"{secilenCari}";
void changeCariText (bool mbool ) {
Console.WriteLine("change cari work");
if (mbool==true)
{
var adi = Preferences.Get("selectedCariAdi", "");
var kodu = Preferences.Get("selectedCariKodu", "");
secilenCari = $"{kodu} {adi}";
Console.WriteLine("cari liste close " + secilenCari);
OnPropertyChanged(nameof(SearchCari));
Console.WriteLine(SearchCari);
}
}
<Label Text="{Binding SearchCari}" x:Name="secilenCariText3" TextColor="Black" FontSize="Medium" WidthRequest="200" HeightRequest="50" HorizontalOptions="Center" VerticalOptions="Center"/>
Edit: Well, i realized that : I can not update label while closing the popup page but when i add button and set buttons command this changeCariText() it updates label.
Update https://www.paste.tc/depoaktarimpageviewmodel Viewmodel
https://www.paste.tc/depoaktarimpagexaml Xaml https://www.paste.tc/caripopuppage 2nd page
Solution : To save something :
CariRoot : Object to send
selectedCari : Selected object to send from this page.
mselectedCari : Key to seperate messages.
MessagingCenter.Send<CariRoot>(selectedCari, "mselectedCari");
To get something you saved :
MessagingCenter.Subscribe<CariRoot>(this, "mselectedCari" ,async (sender) =>
{
SearchCari = $"{sender.CARIUNVAN}";
_SelectedCariRoot = sender;
});
You need to use that code inside viewmodel.
I bind this 2 pages the same viewmodel.
Even if two pages use the same View Model, when two different pages are instantiated, two instances of the same class are created. Therefore, the two pages do not share the same value, even the same property.
But you can pass the data to the Viewmodel instance of the page that you want to update value for the special property.
There are several ways to pass data.
For example, you can use MessagingCenter or EventHandler
to achieve this.
For how to use EventHandler, you can check thread here.
Update:
I add a button to do that then changeCariText() function works fine update my text but when i close pouppage this changeCariText() doesn't update text.
Please recheck if you have implemented interface INotifyPropertyChanged
for your viewmodel and called OnPropertyChanged
for the property you want to update the value and UI of it.
You can refer to the following code:
public class YourViewModel:INotifyPropertyChanged
{
private string _searchCari ;
public string SearchCari
{
set { SetProperty(ref _searchCari, value); }
get { return _searchCari; }
}
void changeCariText (bool mbool ) {
Console.WriteLine("change cari work");
if (mbool==true)
{
var adi = Preferences.Get("selectedCariAdi", "");
var kodu = Preferences.Get("selectedCariKodu", "");
secilenCari = $"{kodu} {adi}";
Console.WriteLine("cari liste close " + secilenCari);
// remove the following code
//OnPropertyChanged(nameof(SearchCari));
//assign value for SearchCari, the UI will update automatically.
SearchCari = yourvalue;
Console.WriteLine(SearchCari);
}
}
// For brevity, omit the other code
public YourViewModel(){
// other code
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
You should still not figure out what the difference between two instances of a class is. Your two pages create two instances of the class DepoAktarımPageViewModel
. You add a Button to page DepoAktarımPage
to change the value of SearchCari
, and you do it on the instance of page DepoAktarımPage
. When you close page CariPopUpPage
, you change the value of SearchCari
on the instance of page CariPopUpPage
. The two instances are unrelated, meaning that the data of the two instances is not shared, even if they are instances of the same class. So changing the value on the ViewModel of page CariPopUpPage
does not affect the data on the ViewModel of page DepoAktarımPage
at all.
For this problem you can use MessagingCenter
or EventHandler
to achieve this.
For more information, you can check my first answer.