Xaml 在Listpicker中加载数据

Xaml 在Listpicker中加载数据,xaml,windows-phone-8,Xaml,Windows Phone 8,请注意,我在listpicker控件中显示数据的方式有问题。我实际上是从web服务加载的,但后来我尝试使用静态数据,但发现无法加载静态数据。它使我的MVVM出现故障。我把代码贴在下面 我的模范班 public class ContinentSelect { public int ID { get; set; } public string Name { get; set; } } 视图模型 public class ContinentViewModel : INotifyPro

请注意,我在listpicker控件中显示数据的方式有问题。我实际上是从web服务加载的,但后来我尝试使用静态数据,但发现无法加载静态数据。它使我的MVVM出现故障。我把代码贴在下面

我的模范班

public class ContinentSelect
{
    public int ID { get; set; }
    public string Name { get; set; }
}
视图模型

public class ContinentViewModel : INotifyPropertyChanged
{
    private ContinentSelect continent;

    // public ContinentViewModel() { }

    public ContinentViewModel(ContinentSelect continent)
    {
        this.continent = continent;
    }

    public string ID
    {
        get
        {
            return continent.ID.ToString();
        }
        set
        {
            if (value != continent.ID.ToString())
            {
                continent.ID =  int.Parse(value);
                NotifyPropertyChanged("ID");
            }
        }
    }

    public string Name
    {
        get
        {
            return continent.Name;
        }
        set
        {
            if (value != continent.Name)
            {
                continent.Name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    //This would notify the view when there is a direct change in the values of the list items
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class MainContinentViewModel  : INotifyPropertyChanged
{
    IRegionService regionservice = null;

    public MainContinentViewModel()
    {
        this.Items = new ObservableCollection<ContinentViewModel>();
        regionservice = new RegionService();
    }

    public ObservableCollection<ContinentViewModel> Items { get; private set; }

    private string _sampleProperty = "Sample Runtime Property Value";
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding
    /// </summary>
    /// <returns></returns>
    public string SampleProperty
    {
        get
        {
            return _sampleProperty;
        }
        set
        {
            if (value != _sampleProperty)
            {
                _sampleProperty = value;
                NotifyPropertyChanged("SampleProperty");
            }
        }
    }

    /// <summary>
    /// Sample property that returns a localized string
    /// </summary>
    public string LocalizedSampleProperty
    {
        get
        {
            return AppResources.SampleProperty;
        }
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }

    public void LoadData() 
    {
       // this.CollectContinents();

        ContinentSelect continent = new ContinentSelect() { ID = 0, Name = "Jide" };
        ContinentViewModel viewmodel = new ContinentViewModel(continent);
        Items.Add(viewmodel);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
公共类视图模型:INotifyPropertyChanged
{
私有大陆选择大陆;
//公共视图模型(){}
公共大陆视图模型(大陆选择大陆)
{
这个大陆=大陆;
}
公共字符串ID
{
收到
{
return containment.ID.ToString();
}
设置
{
if(值!=contraction.ID.ToString())
{
contraction.ID=int.Parse(值);
NotifyPropertyChanged(“ID”);
}
}
}
公共字符串名
{
收到
{
返回大陆。名称;
}
设置
{
if(值!=洲名)
{
名称=值;
NotifyPropertyChanged(“名称”);
}
}
}
//当列表项的值发生直接更改时,这将通知视图
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
公共类主视图模型:INotifyPropertyChanged
{
IRegionService regionservice=null;
公共视图模型()
{
this.Items=新的ObservableCollection();
regionservice=新regionservice();
}
公共可观测集合项{get;private set;}
私有字符串_sampleProperty=“示例运行时属性值”;
/// 
///示例ViewModel属性;此属性在视图中用于使用绑定显示其值
/// 
/// 
公共字符串SampleProperty
{
收到
{
返回sampleProperty;
}
设置
{
如果(值!=\u sampleProperty)
{
_样本属性=值;
NotifyPropertyChanged(“SampleProperty”);
}
}
}
/// 
///返回本地化字符串的示例属性
/// 
公共字符串本地化采样属性
{
收到
{
返回AppResources.SampleProperty;
}
}
公共布尔值已加载
{
收到
私人设置;
}
公共void LoadData()
{
//这个。收集大陆();
大陆选择大陆=新大陆选择(){ID=0,Name=“Jide”};
大陆视图模型viewmodel=新大陆视图模型(大陆);
Items.Add(viewmodel);
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
下面是我的xaml视图。我跳过了一些代码,使其可读性和简短性

<phone:PhoneApplicationPage
    x:Class="JizB2c.UserLocation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DataContext="{d:DesignData SampleData/ContinentSampleData.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    Loaded="UserLocationPage_Loaded">

    <toolkit:ListPicker 
                Grid.Column="0"
                Grid.Row="0"
                Grid.ColumnSpan="2"
                x:Name="listPicker"
                Header="Choose Continent">
        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Orientation="Horizontal">
                    <TextBlock Text="{Binding ID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                </StackPanel>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>
    </toolkit:ListPicker>
</phone:PhoneApplicationPage>


您没有将viewmodel绑定到视图。
这会导致我的MVVM出现故障。
到底出了什么问题?是的,我没有绑定。我刚发现。我已经修好了。我甚至从我的网络服务中收集信息。唯一的问题是当我点击列表框时。而不是显示大陆的列表。它显示了我添加的大陆视图模型。我不知道如何解决此问题。另外,我注意到我正在加载listpicker twicePlease,它正在工作,就在单击listpicker时,我注意到我是一个长列表ContinentViewModel中的模型集合,而不是作为大陆名称的字符串。但是如果我在模型上选择,它会在列表选择器中显示名称