【发布时间】:2018-12-13 07:45:25
【问题描述】:
我正在使用 prism 开发一个安卓应用。
我正在尝试制作 Base ViewModel。在这个 ViewModel 中,我想为我的所有 ViewModel 设置通用属性。
public class BaseViewModel : BindableBase
{
protected INavigationService _navigationService;
protected IPageDialogService _dialogService;
public BaseViewModel(INavigationService navigationService, IPageDialogService dialogService)
{
_navigationService = navigationService;
_dialogService = dialogService;
}
private string _common;
/// <summary>
/// Common property
/// </summary>
public string CommonProperty
{
get { return _common; }
set
{
_common = value;
SetProperty(ref _common, value);
}
}
}
我的问题是:当我在构造函数中设置公共属性时,工作正常。
但是当我在OnNavigatingTo 中设置公共属性并使用异步时,它不起作用。调用OnNavigatingTo 时会触发SetProperty,但我使用此公共属性绑定的标签不会刷新值。
namespace TaskMobile.ViewModels.Tasks
{
/// <summary>
/// Specific view model
/// </summary>
public class AssignedViewModel : BaseViewModel, INavigatingAware
{
public AssignedViewModel(INavigationService navigationService, IPageDialogService dialogService) : base(navigationService,dialogService)
{
CommonProperty= "Jorge Tinoco"; // This works
}
public async void OnNavigatingTo(NavigationParameters parameters)
{
try
{
Models.Vehicle Current = await App.SettingsInDb.CurrentVehicle();
CommonProperty= Current.NameToShow; //This doesn´t works
}
catch (Exception e)
{
App.LogToDb.Error(e);
}
}
}
【问题讨论】:
-
如果我将我的
CommonProperty从BaseViewModel移动到我的AssignedViewModel它可以工作
标签: c# mvvm data-binding prism