导航到详细页面,但在Windows Phone中使用不同的json

时间:2014-02-16 08:03:09

标签: c# json windows-phone-7 windows-phone-8 windows-phone

我想制作一个Windows Phone应用程序,它简要介绍一些新闻。然后,如果我点击某个新闻,它将导航到详细描述。问题是它有不同的json源。

以下是简要说明的来源:http://apibiru.herokuapp.com/v0.1/feeds/1?auth_token=64d362d2e483e8023c46595f83ca8d9555ff6d7cc700a2474fbdbd341c43c1fb

以下是详细说明的来源:http://apibiru.herokuapp.com/v0.1/snack/1?auth_token=64d362d2e483e8023c46595f83ca8d9555ff6d7cc700a2474fbdbd341c43c1fb(将零食后的数字改为不同的数字以查看其他对象。

这是我的MainPage.xaml:

        <phone:Pivot Title="Plumber">
        <!--Pivot item one-->
        <phone:PivotItem Header="readnow">
            <!--Double line list with text wrapping-->
            <phone:LongListSelector x:Name="FeedsLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding Feeds.RootObject}" SelectionChanged="FeedsLongListSelector_SelectionChanged">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17">
                            <TextBlock Text="{Binding main_text}" TextWrapping="Wrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <Image Source="{Binding main_image_url}" VerticalAlignment="Top"></Image>
                            <TextBlock Text="{Binding type}" TextWrapping="Wrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>

这是我的MainPage.xaml.cs:

        private void LoadDataReadnow()
    {
        WebClient readnowClient = new WebClient();
        readnowClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(readnow_DownloadStringCompleted);
        readnowClient.DownloadStringAsync(new Uri("http://apibiru.herokuapp.com/v0.1/feeds/1?auth_token=64d362d2e483e8023c46595f83ca8d9555ff6d7cc700a2474fbdbd341c43c1fb"));
    }

    private void readnow_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
         try
         {
            string jsonStr = e.Result;
            if (!string.IsNullOrEmpty(jsonStr))
            {
                JArray array = JArray.Parse(jsonStr);
                Feeds.RootObject[] feeds = new Feeds.RootObject[3];
                List<Feeds.RootObject> listFeeds = array.ToObject<List<Feeds.RootObject>>();
                FeedsLongListSelector.ItemsSource = listFeeds;
            }
         }

         catch (Exception ex)
         {
            MessageBox.Show("Please check your internet connection");
         }
    }

这里是Feeds.cs的模型:

public class Feeds
{
    public class Topic
    {
        public string name { get; set; }
        public string screen_name { get; set; }
        public int id { get; set; }
    }

    public class RootObject
    {
        public int updated_at { get; set; }
        public string type { get; set; }
        public List<Topic> topics { get; set; }
        public string main_text { get; set; }
        public string uuid { get; set; }
        public Uri main_image_url { get; set; }
        public int id { get; set; }
        public bool is_rumour { get; set; }
        public int created_at { get; set; }
    }

最后一个是Snacks的模型:

public class Snack
{
    public class Topic
    {
        public string name { get; set; }
        public string screen_name { get; set; }
        public int id { get; set; }
    }

    public class RootObject
    {
        public int updated_at { get; set; }
        public string type { get; set; }
        public List<Topic> topics { get; set; }
        public string main_text { get; set; }
        public string uuid { get; set; }
        public string main_image_url { get; set; }
        public int id { get; set; }
        public bool is_rumour { get; set; }
        public int created_at { get; set; }
    }
}

这是我尝试导航它的代码(它不起作用):

        private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (FeedsLongListSelector.SelectedItem == null)
            return;

        NavigationService.Navigate(new Uri("/View/ReadnowDetailPage.xaml?selectedItem=" + (FeedsLongListSelector.SelectedItem as ReadnowViewModel).ID, UriKind.Relative));
    }

感谢您的帮助:)

这是错误:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Plumber
  StackTrace:
       at Plumber.MainPage.FeedsLongListSelector_SelectionChanged(Object sender, SelectionChangedEventArgs e)
       at Microsoft.Phone.Controls.LongListSelector.set_SelectedItem(Object value)
       at Microsoft.Phone.Controls.LongListSelector.OnItemTap(Object sender, GestureEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException: 

这是ReadnowViewModel:

public class ReadnowViewModel : INotifyPropertyChanged
{
    private int _updated_at;
    private string _type;
    private List<Feeds.Topic> _topics;
    private string _main_text;
    private string _uuid;
    private Uri _main_image_url;
    private int _id;
    private bool _is_rumour;
    private int _created_at;

    public int Updated_At
    {
        get
        {
            return this._updated_at;
        }
        set
        {
            if (value != this._updated_at)
            {
                this._updated_at = value;
                NotifyPropertyChanged("Updated_At");
            }
        }
    }


    public string Type
    {
        get
        {
            return this._type;
        }
        set
        {
            if (value != this._type)
            {
                this._type = value;
                NotifyPropertyChanged("Type");
            }
        }
    }

    public List<Feeds.Topic> Topics
    {
        get
        {
            return this._topics;
        }
        set
        {
            if (value != this._topics)
            {
                this._topics = value;
                NotifyPropertyChanged("Topic");
            }
        }
    }

    public string Main_Text
    {
        get
        {
            return this._main_text;
        }
        set
        {
            if (value != this._main_text)
            {
                this._main_text = value;
                NotifyPropertyChanged("Main_Text");
            }
        }
    }


    public string UUID
    {
        get
        {
            return this._uuid;
        }
        set
        {
            if (value != this._uuid)
            {
                this._uuid = value;
                NotifyPropertyChanged("UUID");
            }
        }
    }

    public Uri Main_Image_URL
    {
        get
        {
            return this._main_image_url;
        }
        set
        {
            if (value != this._main_image_url)
            {
                this._main_image_url = value;
                NotifyPropertyChanged("Main_Image_URL");
            }
        }
    }

    public int ID
    {
        get
        {
            return this._id;
        }
        set
        {
            if (value != this._id)
            {
                this._id = value;
                NotifyPropertyChanged("ID");
            }
        }
    }

    public bool Is_Rumour
    {
        get
        {
            return this._is_rumour;
        }
        set
        {
            if (value != this._is_rumour)
            {
                this._is_rumour = value;
                NotifyPropertyChanged("Is_Rumour");
            }
        }
    }

    public int Created_At
    {
        get
        {
            return this._created_at;
        }
        set
        {
            if (value != this._created_at)
            {
                this._created_at = value;
                NotifyPropertyChanged("Created_At");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

查看代码的这一部分:

List<Feeds.RootObject> listFeeds = array.ToObject<List<Feeds.RootObject>>();
FeedsLongListSelector.ItemsSource = listFeeds;

似乎FeedsLongListSelector包含Feeds.RootObject而不是ReadnowViewModel的列表。因此,我建议尝试更改此行:

NavigationService.Navigate(
                new Uri("/View/ReadnowDetailPage.xaml?selectedItem=" + 
                            (FeedsLongListSelector.SelectedItem as ReadnowViewModel).ID
                        , UriKind.Relative));

到此(请注意SelectedItem已投放到RootObject):

NavigationService.Navigate(
                new Uri("/View/ReadnowDetailPage.xaml?selectedItem=" + 
                            (FeedsLongListSelector.SelectedItem as Feeds.RootObject).id
                        , UriKind.Relative));