Page.cs类到App.cs类

时间:2013-08-15 06:23:34

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

我遇到了一个问题,我在App.cs上创建了一个类:

public class Article
{
    public string Text { get; set; }
    public int Id { get; set; }
    public long Date { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public string ImageURL { get; set; }
}

一个公共变量:

public Article ToArticlePage { get; set; }

在NewsPage上,我有同一篇文章:

public class Article
    {
        [JsonProperty("text")]
        public string Text { get; set; }
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("date")]
        public long Date { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("author")]
        public string Author { get; set; }
        [JsonProperty("imageURL")]
        public string ImageURL { get; set; }
    }

然后我正在尝试这样做:

在选择更改时,我正在解析文章ID,并在文章列表(NewsList)中找到它。并将其赋予全局变量ToArticlePage。

   private void NewsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        NLBI = (NewsListBoxItem)NewsListBox.SelectedItem;
        Predicate <Article> articleFinder = (Article p) => { return p.Id == int.Parse(NLBI.id.Text); };
        (App.Current as App).ToArticlePage = NewsList.Result.Articles.Find(articleFinder);
    }

错误:

Error   7   Cannot convert type 'WP8Release2.NotAuthorizedPages.HomePage.Article' to 'WP8Release2.Article'  C:\Users\4\Documents\Visual Studio 2012\Projects\WP8Release2\WP8Release2\NotAuthorizedPages\HomePage.xaml.cs    348 50  WP8Release2

Error   12  Cannot implicitly convert type 'WP8Release2.NotAuthorizedPages.HomePage.Article' to 'WP8Release2.Article'   C:\Users\4\Documents\Visual Studio 2012\Projects\WP8Release2\WP8Release2\NotAuthorizedPages\HomePage.xaml.cs    348 50  WP8Release2

1 个答案:

答案 0 :(得分:1)

听起来你需要创建一个alias(在你的cs文件的顶部)

using HPArticle = WP8Release2.NotAuthorizedPages.HomePage.Article;

然后在你的程序中通过

调用此类文章
private void NewsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    NLBI = (NewsListBoxItem)NewsListBox.SelectedItem;
    Predicate <HPArticle> articleFinder = (HPArticle p) => { return p.Id == int.Parse(NLBI.id.Text); };
    (App.Current as App).ToArticlePage = NewsList.Result.Articles.Find(articleFinder);
}

否则,您需要在任何位置使用完整路径(WP8Release2.NotAuthorizedPages.HomePage.Article)

您遇到的错误是说您的课程中使用了两种不同类型的Article,并且它不知道您要使用哪种类型