Page.cs clases to App.cs clases

本文关键字:cs clases App to Page | 更新日期: 2023-09-27 18:27:56

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

Page.cs clases to App.cs clases

听起来你需要创建一个别名(在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,它不知道你想使用哪一种