我正在制作一個首選項中心,它需要為兩個具有相同布林值/切換值的獨立 Switch 切換同步更新。我在 C# 中使用 Xamarin Forms。我有一個ViewModel.cs檔案,例如
namespace XamarinDemoApp
public class MyViewModel : INotifyPropertyChanged
{
private bool swithOne;
public bool SwithOne
{
set
{
if (swithOne != value)
{
swithOne = value;
OnPropertyChanged("SwithOne");
}
}
get
{
return swithOne;
}
}
public MyViewModel()
{
SwithOne = true; // assign a value for `SwithOne `
}
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;
}
}
然后我的AllowSaleToggleTab.xaml.cs看起來像
namespace XamarinDemoApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AllowSaleToggleTab : ContentPage
{
MyViewModel myViewModel;
public AllowSaleToggleTab()
{
InitializeComponent();
myViewModel = new MyViewModel();
BindingContext = myViewModel;
}
}
}
另一個切換選項卡是PC.xaml.cs
namespace XamarinDemoApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PC : ContentPage
{
MyViewModel myViewModel;
public PC()
{
InitializeComponent();
Console.WriteLine("PC Initialized");
myViewModel = new MyViewModel();
BindingContext = myViewModel;
}
}
}
最后,我隨附的PC.xaml和AllowSaleToggleTab.xaml檔案都有元素
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:xamarindemoapp="clr-namespace:XamarinDemoApp" x:DataType="xamarindemoapp:MyViewModel"
x:Class="XamarinDemoApp.AllowSaleToggleTab">
<Switch x:Name="ToggleSwitch1" IsToggled="{Binding SwithOne}"/>
然而他們仍然不同步。誰能指出我做錯了什么?謝謝
uj5u.com熱心網友回復:
我不知道你的代碼是如何使用的,但是有幾種方法可以實作這一點。
例如,您可以在導航時在兩個不同的 Pages 之間傳遞資料或使用Xamarin.Forms MessagingCenter在兩個頁面之間發送資料訊息。
可以參考以下代碼(我結合了上面兩種方法):
測驗頁1.xaml
<ContentPage.Content>
<StackLayout>
<Switch x:Name="toggleSwitch1" IsToggled="{Binding SwithOne}" HorizontalOptions="Center">
</Switch>
<Button Text="navigate to Page2" Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
TestPage1.xaml.cs
public partial class TestPage1 : ContentPage
{
MyViewModel myViewModel;
public TestPage1()
{
InitializeComponent();
myViewModel = new MyViewModel();
BindingContext = myViewModel;
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync( new TestPage2(myViewModel.SwithOne));//myViewModel.SwithOne
}
}
視圖模型.cs
public class MyViewModel: INotifyPropertyChanged
{
private bool _swithOne { get; set; }
public bool SwithOne
{
set
{
if (_swithOne != value)
{
_swithOne = value;
OnPropertyChanged("SwithOne");
}
}
get
{
return _swithOne;
}
}
public MyViewModel()
{
SwithOne = true;
MessagingCenter.Subscribe<object, object>(this, "PassDataToOne", (sender, args) =>
{
bool value = (bool)args;
if (value!= SwithOne) {
SwithOne = value;
}
});
}
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;
}
測驗頁2.xaml
<ContentPage.Content>
<StackLayout>
<Switch x:Name="toggleSwitch2" IsToggled="{Binding SwithTwo}" HorizontalOptions="Center" />
<Button Text="Navigate To Page 1" Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
TestPage2.xaml.cs
public partial class TestPage2 : ContentPage
{
MyViewModel2 myViewModel;
public TestPage2(bool isToggled)
{
InitializeComponent();
myViewModel = new MyViewModel2(isToggled);
BindingContext = myViewModel;
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
}
MyViewModel2.cs
public class MyViewModel2: INotifyPropertyChanged
{
private bool _swithTwo;
public bool SwithTwo
{
set
{
if (_swithTwo != value)
{
_swithTwo = value;
OnPropertyChanged("SwithTwo");
MessagingCenter.Send<object, object>(this, "PassDataToOne", _swithTwo);
}
}
get
{
return _swithTwo;
}
}
public MyViewModel2( bool isToggled)
{
SwithTwo = isToggled;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
筆記:
1.我為兩個頁面(TestPage1和TestPage2)使用了兩個不同的ViewModel ,TestPage1的ViewModel是MyViewModel,TestPage1的ViewModel是MyViewModel2。
2.After變化的狀態Switch中TestPage1,我們可以通過數值SwithOne中MyViewModel.cs,以TestPage2由建構式:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync( new TestPage2(myViewModel.SwithOne));//myViewModel.SwithOne
}
3.如果我們改變SwithTwoin的值TestPage2,我們可以使用MessagingCenter將值發送到TestPage1:
public class MyViewModel2: INotifyPropertyChanged
{
private bool _swithTwo;
public bool SwithTwo
{
set
{
if (_swithTwo != value)
{
_swithTwo = value;
OnPropertyChanged("SwithTwo");
MessagingCenter.Send<object, object>(this, "PassDataToOne", _swithTwo);
}
}
get
{
return _swithTwo;
}
}
// other code
}
uj5u.com熱心網友回復:
我知道的最簡單的方法是使用靜態物件。
變數持有人:
public class SomeClass : INotifyPropertyChanged
{
public static SomeClass Instance {get;set;} = new SomeClass();
public string SharedString {get;set;}
#region The rest of property changed
...
#endregion
}
查看 1
<Label Text={Binding Path=SharedString, Source={x:Static locationOfSomeClass:SomeClass.Instance}} />
查看 2
<Label Text={Binding Path=SharedString, Source={x:Static locationOfSomeClass:SomeClass.Instance}} />
每當您更改字串時,例如 SomeClass.Instance.SharedString = "I'm update this string"; 您會在兩個位置獲得更新的字串。確保您已安裝 propertychanged.fody nuget。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363509.html
標籤:C# xaml xamarin.forms 安卓系统 切换
