Ash*_*Ash 6 c# xamarin.ios mvvmcross xamarin.forms
我将我的Xamarin.Forms.ContentPage的标题绑定到BuggyTitle我的视图模型(VM)中的属性.VM派生自MvxViewModel.这是简化版本:
BuggyPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<local:ContentPage Title="{Binding BuggyTitle}"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.BuggyPage"
xmlns:local="clr-namespace:Xamarin.Forms;assembly=MyProject">
<ContentPage.Content NavigationPage.HasNavigationBar="false">
<Grid>
<ScrollView>
<!--and so on-->
</ContentPage.Content>
</local:ContentPage>
Run Code Online (Sandbox Code Playgroud)
BuggyViewModel.cs:
namespace MyProject
{
[ImplementPropertyChanged]
public class BuggyViewModel : MvxViewModel
{
private Random _random;
public string BuggyTitle {get; set;}
public BuggyViewModel()
{
_random = new Random();
}
public override void Start()
{
base.Start();
BuggyTitle = "" + _random.Next(1000);
RaisePropertyChanged("BuggyTitle"); // this seems to make no difference
}
}
}
Run Code Online (Sandbox Code Playgroud)
除了InitializeComponent()在构造函数中调用之外,代码中没有太多内容.
页面一般映射到我的项目中的VM(实际上不是'我的'项目,它是现有的设计),它归结为这些(再次,简化的)代码行:
public static Page CreatePage(MvxViewModelRequest request)
{
var viewModelName = request.ViewModelType.Name;
var pageName = viewModelName.Replace ("ViewModel", "Page");
var pageType = (typeof (MvxPagePresentationHelpers)).GetTypeInfo ().Assembly.CreatableTypes().FirstOrDefault(t => t.Name == pageName);
var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader>();
var viewModel = viewModelLoader.LoadViewModel(request, null);
var page = Activator.CreateInstance(pageType) as Page;
page.BindingContext = viewModel;
return page;
}
Run Code Online (Sandbox Code Playgroud)
问题:
当BuggyPage负载,我最初得到标题正确的值.每当它显示之后,即使我可以在调试器中看到BuggyTitle正确更新,但更改不会出现在页面中.
题:
为什么不在BuggyTitle页面中反映更新?
编辑1:
为了进一步说明怪事,我加了Label我ContentPage,与x:Name="BuggyLabel"和Text="{Binding BuggyLabelText}".在我的代码隐藏中,我添加了这个:
var binding_context = (BindingContext as BuggyViewModel);
if (binding_context != null)
{
BuggyLabel.Text = binding_context.BuggyLabelText;
}
Run Code Online (Sandbox Code Playgroud)
我设定了一个断点BuggyLabel.Text =.每次页面加载时它都会被击中,并且BuggyLabel.Text似乎已经具有正确的值(即,无论binding_context.BuggyLabelText设置为什么).但是,仅显示的实际页面会显示此标签中的文本最初设置为的内容.
是的,干净/建造了大约一百万次.
编辑2(更奇怪):
我把它放在代码隐藏中,以便它在页面加载期间运行:
var binding_context = (BindingContext as BuggyViewModel);
if (binding_context != null)
{
Device.BeginInvokeOnMainThread(() =>
{
binding_context.RefreshTitleCommand.Execute(null);
});
}
Run Code Online (Sandbox Code Playgroud)
这再次更改调试器中的值,但这些更改不会反映在显示的页面中.
然后我在页面上添加了一个按钮并将其绑定到了RefreshTitleCommand,并且!页面更新其显示.
不幸的是我不能用这个.它不仅令人难以置信的hackish,我不能让用户按下按钮让页面显示它在加载时的意义.
我想知道MvvmCross或Xamarin是否有一些缓存.
您需要在BuggyTitle属性声明中添加RaisePropertyChanged.
namespace MyProject
{
[ImplementPropertyChanged]
public class BuggyViewModel : MvxViewModel
{
private Random _random;
string _BuggyTitle { get; set; }
public string BuggyTitle
{
get { return _BuggyTitle; }
set { _BuggyTitle = value; RaisePropertyChanged(() => BuggyTitle); }
}
public BuggyViewModel()
{
_random = new Random();
}
public override void Start()
{
base.Start();
BuggyTitle = "" + _random.Next(1000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
var binding_context = (BindingContext as BuggyViewModel);
if (binding_context != null)
{
Device.BeginInvokeOnMainThread(() =>
{
BuggyLabel.Text = binding_context.BuggyLabelText;
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5920 次 |
| 最近记录: |