0

我遇到了一个问题,我在 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
4

1 回答 1

1

听起来您需要创建一个别名(在您的 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你的班级有两种不同的类型,它不知道你想使用哪一种

于 2013-08-15T06:28:29.323 回答