通过将Xamarin.Forms与MVVM一起使用,我具有带有StoryName,StoryCategories和Popup(按钮)的故事的ListView。单击“弹出”按钮时,将出现一个弹出窗口,用户可以在其中为故事选择不同的类别。并且,当用户选择其他类别时,StoryCategories标签应使用所有选定类别(逗号分隔)进行更新。但是我不知道如何从单击弹出按钮的位置找到并更新StoryCategories标签?
过去8年来,我一直在开发.net mvc,我始终能够在StackOverflow上找到我的问题的答案(谢谢!),但是由于某些原因,我似乎无法找到该问题的答案。
希望您能提供帮助! :)
弹出窗口是Rg.Plugins.Popup,我正在使用James Montemagno的MvvmHelpers。
型号
public class Story : ObservableObject
{
string name;
public string Name
{
get { return name; }
set { SetProperty(ref name, value); }
}
string categories;
public string Categories
{
get { return categories; }
set { SetProperty(ref categories, value); }
}
}
ViewModel
public class StoriesViewModel : BaseViewModel
{
public ObservableCollection<Story> Stories { get; set; }
public StoriesViewModel()
{
Stories = new ObservableCollection<Story>();
Stories.Add(new Story { Name = "Hitchhiker's Guide to the Galaxy", Categories = "a, b" });
Stories.Add(new Story { Name = "The Restaurant at the End of the Universe", Categories = "b, c, y" });
Stories.Add(new Story { Name = "Life, the Universe and Everything", Categories = "e" });
Stories.Add(new Story { Name = "So Long, and Thanks for All the Fish", Categories = "a, c, e" });
}
public ICommand OpenPopupCommand
{
get
{
return new Command<Story>((x) =>
{
Debug.WriteLine("Name: " + x.Name);
});
}
}
}
查看:
<ListView ItemsSource="{Binding Stories}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}" />
<Label Text="{Binding Categories} "/>
<Button Text="Click" Command="{ Binding Path=BindingContext.OpenPopupCommand, Source={ x:Reference Stories } }" CommandParameter="{ Binding . }" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:0)
如果您使用clicked事件,则可以执行以下操作,我只是猜测您使用的类的名称,但您应该得到它:
var story = ((Button)sender).BindingContext as Story;
答案 1 :(得分:0)
如果您使用的是MVVM模式,请尝试以下操作:
{
"name": "acbook_angular",
"private": true,
"dependencies": {
"@angular/common": "^7.2.8",
"@angular/compiler": "^7.2.8",
"@angular/core": "^7.2.8",
"@angular/platform-browser": "^7.2.8",
"@angular/platform-browser-dynamic": "^7.2.8",
"@angular/router": "^7.2.10",
"@rails/webpacker": "^4.0.2",
"core-js": "^2.6.5",
"html-loader": "^0.5.5",
"rxjs": "^6.4.0",
"ts-loader": "^5.3.3",
"typescript": "^3.3.3333",
"zone.js": "^0.8.29"
},
"devDependencies": {
"css-loader": "^2.1.1",
"resolve-url-loader": "^3.0.1",
"sass-loader": "^7.1.0",
"to-string-loader": "^1.1.5",
"webpack-dev-server": "^3.2.1",
"webpack-merge": "^4.2.1"
}
}
在视图模型中
<ListView x:Name="Stories">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding StoryName}" />
<Label Text="{Binding StoryCategories} "/>
<Button Command="{ Binding Path=BindingContext.OpenPopupCommand, Source={ x:Reference Stories } }" CommandParameter="{ Binding . }" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 2 :(得分:0)
我过去解决此问题的方法是将整个故事页面ViewModel传递到弹出页面,然后在更改类别时,可以从弹出页面视图模型更新故事页面中的标签。我不知道这是否是最好的解决方案,但是它确实有效。