【问题标题】:How can I bind data into Xaml, Windows Phone?如何将数据绑定到 Xaml、Windows Phone?
【发布时间】:2013-01-04 08:32:24
【问题描述】:

这是我的 C# 代码,现在它在控制台中显示数据。

class PlaceViewModel
    {

        public List<Vantaa.IntroPage.Place> Places;
    }       

public class Place
            {
                public string id { get; set; }
                public string title { get; set; }
                public string latitude { get; set; }
                public string longitude { get; set; }
                public string www { get; set; }
            }

        public class RootObject
        {
            public List<Place> Places { get; set; }
        }

    protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place"));
        }

        private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);

            foreach (var book in rootObject.Places)
            {
                System.Diagnostics.Debug.WriteLine(book.id);
            }
            this.DataContext = new PlaceViewModel
                 {
                      Places = rootObject.Places
                 };
            }

我应该如何处理 xaml 文件以在文本块中显示数据?

这是我当前的 xaml 代码。它肯定行不通。我真的不知道。

<ListBox ItemsSource="{Binding Places}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Path=id}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

【问题讨论】:

    标签: json windows-phone-7 xaml data-binding


    【解决方案1】:

    您的类代码看起来不错,所以现在只需将结果绑定到视图即可。您正在返回一个对象列表,因此您应该使用支持显示多个项目的控件。当然,您可以将书籍的所有 id 连接到一个字符串并在标签中显示。但这不是它的完成方式。您应该做的是向 XAML 添加一个 ListBox 控件并在其中创建一个 DataTemplate。通过这种方式,您可以设置项目的显示方式。

    创建一个类,作为 XAML 页面的 ViewModel。此类将具有属性“Places”(类型:List&lt;Place&gt;)。在 OnNavigatedTo-event 中获取所有数据完成后,填充 ViewModel 并绑定绑定到 XAML 的 DataContext:

    this.DataContext = new YourViewModel { Places = rootObject.Places };
    

    这样您就可以从 XAML 中的 ViewModel 中获取所有对象:

    <ListBox ItemsSource="{Binding Places}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding Path=id}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    编辑:

    这是一个工作示例:

    XAML:

    <Grid>
        <ListBox ItemsSource="{Binding Places}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=ID}" />
                        <TextBlock Text="{Binding Path=Title}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

    Place.cs:

    public class Place
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string Web { get; set; }
    }
    

    MainViewModel.cs:

    public class MainViewModel
    {
        public MainViewModel()
        {
            Places = new List<Place>();
        }
    
        public List<Place> Places { get; set; }
    }
    

    示例存储库(您应该有自己的):

    public static List<Place> FetchData()
    {
        var lst = new List<Place>();
    
        lst.Add(new Place { ID = "1", Title = "One", Latitude = "111", Longitude = "111", Web = "www......" });
        lst.Add(new Place { ID = "2", Title = "Two", Latitude = "222", Longitude = "222", Web = "www......" });
        lst.Add(new Place { ID = "3", Title = "Three", Latitude = "333", Longitude = "333", Web = "www......" });
        lst.Add(new Place { ID = "4", Title = "Four", Latitude = "444", Longitude = "444", Web = "www......" });
    
        return lst;
    }
    

    MainWindow.xaml.cs:

    public MainWindow()
    {
        InitializeComponent();
        //This is where the magic happens
        //Fill the viewModel with the data
        var viewModel = new MainViewModel { Places = Repository.FetchData() };
        //Assign the viewModel with the data to the DataContext
        //The bindings will be automatically done in the XAML
        DataContext = viewModel;
    }
    

    【讨论】:

    • 您创建了一个 ViewModel 类吗?您是否用获取的数据填充了属性?你把 ViewModel 分配给 DataContext 了吗?
    • 伙伴,您展示了一个示例存储库,并告诉您拥有自己的存储库。我怎样才能用动态数据填充它?
    • 这是一个单独问题的材料... :D 无论如何,您可以拥有一个数据库并使用像实体框架这样的对象关系映射器,这会从您的表中创建对象。这样您就可以通过 LinQ 与对象进行通信。存储库是一个类,具有用于添加/更新/删除数据的各种方法。希望这会有所帮助:)
    【解决方案2】:

    您的 PlaceViewModel 类应实现 INotifyProprtyChanged 接口,并且您要绑定的所有属性都应在设置器中通知它们的更改。

    为了能够更新绑定文本块中的值,属性可以通知其更改。

    示例: http://msdn.microsoft.com/en-us/library/ms229614.aspx

    【讨论】:

    • +1 用于实现 INotifyPropertyChanged 的​​提示,但我认为在这种情况下基本绑定就可以了。但是,当您进入更高级别的绑定时,您是绝对正确的! :)
    猜你喜欢
    • 2014-03-30
    • 2023-03-06
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多