Mvvm 将城市绑定到AutoSuggestBox
我正在尝试学习MVVVM,通过做一个天气应用程序,使用Udemy课程作为参考,Bing地图Api和OpenWeather Api 我试图用我的xaml的AutoSuggestBox文本绑定城市 我已经完成了WeatherVM并将其作为页面资源绑定到视图 我还做了一个快速的方法,让我知道我所在的城市和国家代码(使用Bing地图api) 我从MainPage.cs调用了这个方法,只是想看看它是否有效,而且效果很好。我得到了预期的城市和国家代码 天气预报员Mvvm 将城市绑定到AutoSuggestBox,mvvm,uwp,Mvvm,Uwp,我正在尝试学习MVVVM,通过做一个天气应用程序,使用Udemy课程作为参考,Bing地图Api和OpenWeather Api 我试图用我的xaml的AutoSuggestBox文本绑定城市 我已经完成了WeatherVM并将其作为页面资源绑定到视图 我还做了一个快速的方法,让我知道我所在的城市和国家代码(使用Bing地图api) 我从MainPage.cs调用了这个方法,只是想看看它是否有效,而且效果很好。我得到了预期的城市和国家代码 天气预报员 public OpenWe
public OpenWeather OpenWeather { get; set; }
private Task<string> _city;
public Task<string> city {
get { return _city; }
set {
_city = value;
GetLocationData();
}
}
public WeatherVM() {
OpenWeather = new OpenWeather();
}
private async void GetLocationData() {
var cityData = await MapLocator.GetCityData();
}
}
}
公共OpenWeather OpenWeather{get;set;}
私人任务(城市),;
公共任务城市{
获取{return\u city;}
设置{
_城市=价值;
GetLocationData();
}
}
公共天气{
OpenWeather=新OpenWeather();
}
私有异步void GetLocationData(){
var cityData=wait MapLocator.GetCityData();
}
}
}
MainPage.xaml
x:Class="MVVM_Example.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MVVM_Example"
xmlns:vm="using:MVVM_Example.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<vm:WeatherVM x:Key="vm" />
</Page.Resources>
<Grid DataContext="{StaticResource vm}">
<AutoSuggestBox Margin="40" QueryIcon="Find"
PlaceholderText="Search"
Text="{Binding Source={StaticResource vm}, Path=city, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Page>
x:Class=“MVVM\u Example.MainPage”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local=“使用:MVVM\u示例”
xmlns:vm=“使用:MVVM\u示例.ViewModel”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable=“d”
后台=“{ThemeResource应用程序页面背景Themebrush}”>
我希望“Orlando”出现在我的AutoSuggestBox中使用绑定时,需要将页面的DataContext设置为绑定源类的实例,而不是设置page.Resource。有关更多详细信息,请参阅此 更新: 您显示的ViewModel存在一些问题。首先,需要为cityData属性指定一个值,否则,与cityData绑定的AutoSuggestBox文本将始终显示为空。其次,您需要在WeatherVM中实现INotifyChanged。当cityData更改时,AutoSuggestBox的文本将更改。第三,不要将属性类型设置为异步类任务,这会影响显示,最好设置字符串cityData
public class WeatherVM : INotifyPropertyChanged
{
public WeatherVM(){
GetData();
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string _cityData;
public string cityData
{
get
{
return _cityData;
}
set
{
_cityData = value;
OnPropertyChanged();
}
}
private async void GetData()
{
cityData = await MapLocator.GetCityData();
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我已经设置了上下文,因为我使用的是MVVM,我不认为代码中有任何内容。这就是我的老师对我的疏忽所说的。您设置的xaml是正确的,但是关于代码隐藏有以下问题。首先,需要为city属性指定一个值,否则,AutoSuggestBox的文本将始终显示为空。您应该设置x:name而不是x:key,这样您就可以在.cs中使用它,并像这样分配它:
this.vm.city=其次,您需要在WeatherVM中实现INotifyChanged。当您的城市发生变化时,AutoSuggestBox的文本将发生变化。第三,不要将属性类型设置为异步任务,这会影响显示,最好设置字符串city
。我在WeatherVM中实现了INotifyChanged,并将其添加到AutoSuggestBoxText=“{Binding Path=cityData}
中,在我的WeatherVM中,我有公共字符串cityData{get]{return{u cityData;}set{{u cityData=value;GetLocationData();}
我不认为代码隐藏中有任何内容。在MVVM中,视图不必知道数据来自何处,我的方法GetLocation
返回城市和国家代码在本教程中,他没有任何代码隐藏。当然,他使用的是框架,但我正在手动操作您提供的视频d不是UWP教程。参考的意义不太好。你可以参考它来了解MVVM。此外,你能提供一个简单的示例来重现这个问题吗?因为据我所知,我看不到你给你的cityData赋值。cityData仍然是空的,所以“Orlando”不能出现在AutoSuggestBox中。
this.viewModelInDataContext.city = "Orlando";
public class WeatherVM : INotifyPropertyChanged
{
public WeatherVM(){
GetData();
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string _cityData;
public string cityData
{
get
{
return _cityData;
}
set
{
_cityData = value;
OnPropertyChanged();
}
}
private async void GetData()
{
cityData = await MapLocator.GetCityData();
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}