[英]Binding to pseudo-static data in Windows Phone 8.1 with MVVM Light
To try and minimise the amount of code, I will try to reduce the project into four files, and also only concern ourselves with the text property (if we can get text working, we can get colour working too). 为了尽量减少代码量,我将尝试将项目缩减为四个文件,并且仅关注text属性(如果可以使文本工作,也可以使颜色工作)。 Hopefully we can assume my ViewModelLocator.cs, App.xaml, Generic.xaml and the supporting ConnectivityStatusBar.cs have been correctly implemented (I can share these too if necessary). 希望我们可以假设我的ViewModelLocator.cs,App.xaml,Generic.xaml和支持的ConnectivityStatusBar.cs已正确实现(必要时我也可以共享)。
<Page
x:Class="Client.View.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:customElements="using:Client.CustomElements"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource ViewModelLocator}}">
<Grid>
<StackPanel>
<customElements:ConnectivityStatusBar
Text="{Binding ConnectivityStatusBar.Text,
Source={StaticResource ViewModelLocator}}" />
<Button Content="Invert"
Command="{Binding HomePage.InvertConnectivityCommand}" />
</StackPanel>
</Grid>
</Page>
using Client.ViewModel;
using GalaSoft.MvvmLight;
namespace Client
{
public sealed class SessionData : ViewModelBase
{
private static readonly SessionData Instance = new SessionData();
private bool _online;
public static SessionData getInstance
{
get { return Instance; }
}
public bool Online
{
get { return _online; }
set
{
_online = value;
RaisePropertyChanged(() => Online);
RaisePropertyChanged(() => ConnectivityStatusBarViewModel.getInstance.Text);
}
}
}
}
using System;
using GalaSoft.MvvmLight;
namespace Client.ViewModel
{
public sealed class ConnectivityStatusBarViewModel : ViewModelBase
{
private static readonly ConnectivityStatusBarViewModel Instance = new ConnectivityStatusBarViewModel();
public static ConnectivityStatusBarViewModel getInstance
{
get { return Instance; }
}
public String Text
{
get { return SessionData.getInstance.Online ? "Online" : "Offline"; }
}
}
}
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace Client.ViewModel
{
public class HomePageViewModel : ViewModelBase
{
public HomePageViewModel()
{
InvertConnectivityCommand = new RelayCommand(InvertConnectivity);
}
public RelayCommand InvertConnectivityCommand { get; set; }
private void InvertConnectivity()
{
SessionData.getInstance.Online = !SessionData.getInstance.Online;
}
}
}
This code doesn't give me the results I want; 这段代码没有给我想要的结果; when the button is pressed on HomePage.xaml, the text does not change. 当在HomePage.xaml上按下按钮时,文本不会更改。 It does work if I put the inversion method into ConnectivityStatusBarViewModel.cs. 如果将反转方法放入ConnectivityStatusBarViewModel.cs中,它将起作用。
Is it possible to make this work, or am I wasting my time? 是否可以进行这项工作,还是我在浪费时间? If it is possible, how can I change my approach to make it work? 如果有可能,我如何更改我的方法以使其起作用?
Edit : ViewModelLocator.cs looks like: 编辑 :ViewModelLocator.cs看起来像:
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
namespace Client.ViewModel
{
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<ConnectivityStatusBarViewModel>();
SimpleIoc.Default.Register<HomePageViewModel>();
}
public ConnectivityStatusBarViewModel ConnectivityStatusBar
{
get { return ServiceLocator.Current.GetInstance<ConnectivityStatusBarViewModel>(); }
}
public HomePageViewModel HomePage
{
get { return ServiceLocator.Current.GetInstance<HomePageViewModel>(); }
}
}
}
Instead of registering new ConnectivityStatusBarViewModel instance in ViewModelLocator, can you try providing the exact instance to it. 您可以尝试向其提供确切的实例,而不是在ViewModelLocator中注册新的ConnectivityStatusBarViewModel实例。
SimpleIoc.Default.Register(() => ConnectivityStatusBarViewModel.getInstance);
This way when the call to get the instance gets invoked: 这样,当获取实例的调用被调用时:
return ServiceLocator.Current.GetInstance<ConnectivityStatusBarViewModel>();
you will actually be returning that instance. 您实际上将返回该实例。
So when you do binding to VMlocator and ConnectivityStatusBar.Text, it should work. 因此,当您绑定到VMlocator和ConnectivityStatusBar.Text时,它应该可以工作。
Text="{Binding ConnectivityStatusBar.Text,
Source={StaticResource ViewModelLocator}}"
You were essentially binding to another instance, which is why the data wasn't refreshed in the UI. 您实际上是在绑定到另一个实例,这就是为什么未在UI中刷新数据的原因。
BTW there are some design issues/redundancies with this/your code, but it's out of the scope of the question - I am trying to make minimal changes to it to get it working. 顺便说一句,此代码/您的代码存在一些设计问题/冗余,但这超出了问题的范围-我正尝试对其进行最小的更改以使其正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.