我試圖通知一個 ViewModel 關于另一個 ViewModel 的更改。對于這個問題,我嘗試使用 MessagingCenter。但是,由于某種原因,它似乎不起作用。有人可以放下一些光,為什么我無法從另一個視圖中更改標簽的顏色?
HomeViewModel.cs:
using System;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MessagingCenterApp.ViewModels
{
public class HomeViewModel : BaseViewModel
{
public HomeViewModel()
{
Title = "Home";
MessagingCenter.Subscribe<Object, Color>(this, "gaugeColor", (sender, arg) =>
{
this._gaugeColor = arg;
});
this.GaugeColor = Color.White;
}
private Color _gaugeColor;
public Color GaugeColor
{
get => _gaugeColor;
set
{
_gaugeColor = value;
OnPropertyChanged();
}
}
}
}
SettingsViewModel.cs:
using MessagingCenterApp.Models;
using MessagingCenterApp.Views;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MessagingCenterApp.ViewModels
{
public class SettingsViewModel : BaseViewModel
{
public SettingsViewModel()
{
Title = "Settings";
MessagingCenter.Send<Object, Color>(this, "gaugeColor", Color.FromHex(this.GaugeColor));
}
private string _gaugeColor;
public string GaugeColor
{
get => Preferences.Get("GaugeColor", "#17805d");
set
{
Preferences.Set("GaugeColor", value);
this.OnPropertyChanged();
}
}
}
}
編輯:
沒有單獨按鈕的實作:
SettingsViewModel.cs:
using MessagingCenterApp.Models;
using MessagingCenterApp.Views;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MessagingCenterApp.ViewModels
{
public class SettingsViewModel : BaseViewModel
{
public ICommand changeCommand { get; set; }
public SettingsViewModel()
{
Title = "Settings";
GaugeColor = Color.Red;
changeCommand = new Command(changeColor);
}
private void changeColor()
{
GaugeColor = Color.FromHex(this.GaugeColorS);
MessagingCenter.Send<Object, Color>(this, "gaugeColor", GaugeColor);
}
private Color _gaugeColor;
public Color GaugeColor
{
get => _gaugeColor;
set
{
_gaugeColor = value;
this.OnPropertyChanged();
}
}
private string _gaugeColorS;
public string GaugeColorS
{
get => Preferences.Get("GaugeColor", "#17805d");
set
{
Preferences.Set("GaugeColor", value);
this.changeColor();
this.OnPropertyChanged();
}
}
}
}
HomeViewModel.cs:
using System;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MessagingCenterApp.ViewModels
{
public class HomeViewModel : BaseViewModel
{
public HomeViewModel()
{
Title = "Home";
MessagingCenter.Subscribe<Object, Color>(this, "gaugeColor", (sender, arg) =>
{
System.Diagnostics.Debug.WriteLine("received color = " arg);
this.GaugeColor = arg;
});
this.GaugeColor = Color.Red;
}
private Color _gaugeColor;
public Color GaugeColor
{
get => _gaugeColor;
set
{
_gaugeColor = value;
OnPropertyChanged();
}
}
}
}
uj5u.com熱心網友回復:
如果您想在當前頁面的屬性被重置時向另一個頁面發送訊息,您應該添加一個事件觸發器(例如單擊頁面中的“按鈕” SettingsPage。單擊按鈕后,向頁面發送訊息HomePage)。
我簡化了你的代碼,它在我這邊作業正常。您可以修改它以滿足您的需要。
BaseViewModel.cs
public class BaseViewModel : INotifyPropertyChanged
{
public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>();
bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
string title = string.Empty;
public string Title
{
get { return title; }
set { SetProperty(ref title, value); }
}
public 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;
}
HomeViewModel.cs
public class HomeViewModel : BaseViewModel
{
private Color _gaugeColor;
public Color GaugeColor
{
set { SetProperty(ref _gaugeColor, value); }
get { return _gaugeColor; }
}
public HomeViewModel()
{
Title = "Home";
MessagingCenter.Subscribe<Object, Color>(this, "gaugeColor", (sender, arg) =>
{
System.Diagnostics.Debug.WriteLine("received color = " arg);
this.GaugeColor = arg;
});
this.GaugeColor = Color.Red;
}
}
SettingsViewModel.cs
public class SettingsViewModel : BaseViewModel
{
public ICommand changeCommand { get; set; }
public SettingsViewModel()
{
Title = "Settings";
GaugeColor = Color.Red;
changeCommand = new Command(changeColor);
}
private void changeColor(object obj)
{
GaugeColor = Color.Yellow;
MessagingCenter.Send<Object, Color>(this, "gaugeColor", GaugeColor);
}
Color _gaugeColor;
public Color GaugeColor
{
set { SetProperty(ref _gaugeColor, value); }
get { return _gaugeColor; }
}
}
SettingsPage.xaml(這里我添加 Button )
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MessagingCenterApp.Views.SettingsPage"
Title="{Binding Title}"
xmlns:vm="clr-namespace:MessagingCenterApp.ViewModels"
Background="#121212"
x:Name="BrowseItemsPage">
<ContentPage.BindingContext>
<vm:SettingsViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Accent">#0d1117</Color>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Orientation="Vertical">
<Label Text="Gauge color:"></Label>
<Entry Text="{Binding GaugeColor}" Keyboard="Numeric"></Entry>
<Button Text="change color" Command="{Binding changeCommand}"/>
</StackLayout>
</ContentPage>
更新:
我的意思是當我在設定頁面上輸入或選擇新顏色時,我希望已經看到新顏色
為此,您可以MessagingCenter.Send按SettingsViewModel如下方式添加:
Color _gaugeColor;
public Color GaugeColor
{
set {
SetProperty(ref _gaugeColor, value);
MessagingCenter.Send<Object, Color>(this, "gaugeColor", GaugeColor);
}
get { return _gaugeColor; }
}
如果你不想要 Button,你可以洗掉Buttonin SettingsPage.xaml。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449248.html
標籤:C# xamarin xamarin.forms 虚拟机
下一篇:在Xamarin中強制執行主題
