Pivot
の項目を持つXAMLページのWindows 10アプリケーションがあり、デフォルトの動作がPivot
の場合、Pivot
のページに移動するたびに、 Pivot
がすぐに選択されます。私がしたいのは、の前にのコードを実行して、Pivot
の最初の項目を実行することです。ページが読み込まれるのを待つ -
コンテキストでコードの問題に関連
public sealed partial class ContentFrame : Page
{
private IMobileServiceTable<News> NewsItems = App.MobileService.GetTable<News>();
private List<News> AllNews;
private List<News> Windows = new List<News>();
private List<News> Apple = new List<News>();
private List<News> Google = new List<News>();
private List<News> Other = new List<News>();
private List<News> Top = new List<News>();
private string WeekID;
public ContentFrame()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
WeekID = e.Parameter as string;
AllNews = await NewsItems.Where(News => News.Week == WeekID).ToListAsync();
separateContent();
}
private void separateContent()
{
foreach (News item in AllNews)
{
Debug.WriteLine(item.Tag);
if (item.Tag == "Windows")
Windows.Add(item);
else if (item.Tag == "Apple")
Apple.Add(item);
else if (item.Tag == "Google")
Google.Add(item);
else if (item.Tag == "Other")
Other.Add(item);
else
Top.Add(item);
}
}
private void Tags_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
IntroHead.FontWeight = FontWeights.Normal;
WindowsHead.FontWeight = FontWeights.Normal;
AppleHead.FontWeight = FontWeights.Normal;
GoogleHead.FontWeight = FontWeights.Normal;
OtherHead.FontWeight = FontWeights.Normal;
switch (Tags.SelectedIndex)
{
case 0:
IntroHead.FontWeight = FontWeights.SemiBold;
StoryContent.Navigate(typeof(TopNewsPage), Top);
break;
case 1:
WindowsHead.FontWeight = FontWeights.SemiBold;
StoryContent.Navigate(typeof(OtherNews), Windows);
break;
case 2:
AppleHead.FontWeight = FontWeights.SemiBold;
StoryContent.Navigate(typeof(OtherNews), Apple);
break;
case 3:
GoogleHead.FontWeight = FontWeights.SemiBold;
StoryContent.Navigate(typeof(OtherNews), Google);
break;
case 4:
OtherHead.FontWeight = FontWeights.SemiBold;
StoryContent.Navigate(typeof(OtherNews), Other);
break;
}
}
}
コード行:
StoryContent.Navigate(typeof演算(TopNewsPage)、トップ)。
は、これはあまりにも早く実行し、私はseparateContent();
方法は、私はアプリは私のXAMLレイアウト上Pivot
コントロールをナビゲートすることができ前に実行されるまで待機します。私がそうでなければ、(Top
)に渡されたパラメータはnull /空になり、私のアプリケーションはTopNewsPage
に移動するとすぐに例外をスローします。
どうすればいいですか?
私は自分のアプリを作っている方法が最善のものではないことを理解しています(MVVMは私が将来探求しようとしているものです)。これは私の最初のアプリであり、 。私はTask
で少し調べましたが、Pivot
ナビゲーションが速すぎるため、何も私の原因を助けてくれないようです。
なぜ、 'InitializeComponent()'の後に 'separateContent();'を呼び出さないのですか? –
良い質問です。私は 'OnContent();'を呼び出す前に 'OnNavigatedTo'メソッドのパラメータで渡された' WeekID'を必要としますので、私の場合は動作しません。 – CStruggle