Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvvm灯光:从视图调用viewmodel中的方法_C#_Windows Phone 8_Mvvm_Mvvm Light_Win Universal App - Fatal编程技术网

C# mvvm灯光:从视图调用viewmodel中的方法

C# mvvm灯光:从视图调用viewmodel中的方法,c#,windows-phone-8,mvvm,mvvm-light,win-universal-app,C#,Windows Phone 8,Mvvm,Mvvm Light,Win Universal App,我正在使用Universal app,实际上,我想从视图调用viewmodel中的一个方法来更新要显示的数据 在我的viewmodel中: public async void GetDateScore(DateTime dt) { string date = dt.Date.ToString("yyyyMMdd"); List<Score> scoreList = await HtmlGetterHelper.GetLastNightScore(date);

我正在使用Universal app,实际上,我想从视图调用viewmodel中的一个方法来更新要显示的数据

在我的viewmodel中:

public async void GetDateScore(DateTime dt)
{
    string date = dt.Date.ToString("yyyyMMdd");
    List<Score> scoreList = await HtmlGetterHelper.GetLastNightScore(date);
    LastScore = new ObservableCollection<Score>();

    for (int i = 0; i < scoreList.Count; i++)
    {
        LastScore.Add(scoreList[i]);
    }
}
public异步void GetDateScore(DateTime dt)
{
字符串日期=dt.date.ToString(“yyyyMMdd”);
List scoreList=等待HtmlGetTerherPer.GetLastNightScore(日期);
LastScore=新的ObservableCollection();
对于(int i=0;i
我尝试用
base.GetDataScore(DateTime.Today)
调用它,但它不起作用。 我不知道是否可以用这样的东西调用该方法,或者我是否应该使用MVVM的messenger?

虽然我认为,您的场景是由于一路上的错误转弯造成的,但您可以使用服务定位器来解决您的问题:

var vm = ServiceLocator.Current.GetInstance<TypeOfObject>();
// do something with viewmodel
当然,您必须先注册:

SimpleIoc.Default.Register<SomeViewModel>();
simpleoc.Default.Register();

如果该视图模型是视图的datacontext,则可以按如下方式执行:

await (DataContext as YourViewModel).GetDataScore(date);
但我建议使用没有代码隐藏的命令:

<interactivity:Interaction.Triggers>
                    <interactivity:EventTrigger EventName="Tap">
                        <interactivity:InvokeCommandAction Command="{Binding GetDataSourceCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=YourTextBox, Path=Text}" />
                    </interactivity:EventTrigger>
<interactivity:Interaction.Triggers>

在viewmodel中:

public RelayCommand<DateTime> GetDataSourceCommand
{
    get { return _getDataSourceCommand ?? (_getDataSourceCommand = new RelayCommand<DateTime>(GetDataSource)); }
}
public RelayCommand GetDataSourceCommand
{
获取{return}
}

好的,我想我可以用这个做点什么,但我只想知道是否有可能做得更好。要明确的是,我有一个html解析器,它获取url并返回一个对象,全天数据(这是一个关于运动成绩的应用程序)都在特定的页面上,就像我在日历上单击不同的日期时,我想更新viewmodel中的数据,你认为有更好的方法吗?谢谢你的回答!如果您有双向绑定,您可以将日历日期绑定到ViewModel中的一个属性,新的日期将在ViewModel中引发
OnPropertyChanged
事件,这样您就可以执行必要的操作,然后更新绑定到
AllDayData
的属性。好的,我想试试这种方法!谢谢!很高兴能帮上忙!
public RelayCommand<DateTime> GetDataSourceCommand
{
    get { return _getDataSourceCommand ?? (_getDataSourceCommand = new RelayCommand<DateTime>(GetDataSource)); }
}