Xamarin初心者、初めての質問です。
分かりにくい部分があるかと思いますが、よろしくお願い致します。
前提・実現したいこと
現在、Xamarinを使用してスマホ向けのアプリケーションを作成しています。
その時に、以下のような動きをするアプリを作成したいのですが、どのようにすれば
良いのでしょうか?
・MainPageからボタンを押してPage1へ遷移する
・Page1にあるボタンを押すとMainPageにあるラベルの文字が変更される
ソースコード
何も工夫せず作成したコードは以下です。
MainPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:App1" x:Class="App1.MainPage"> <StackLayout> <!-Page1へ遷移--> <Button Text="Nex page" Clicked="Button_Clicked" /> <!-変更したいラベル--> <Label x:FieldModifier="friend" x:Name="MainLabel" Text="first label"/> </StackLayout> </ContentPage>
MainPage.xaml.cs
namespace App1 { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } async void Button_Clicked(object sender, System.EventArgs e) { // Page1へ遷移 await Navigation.PushAsync(new Page1()); } } }
Page1.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App1.Page1"> <ContentPage.Content> <StackLayout> <!-MainPageにあるラベルを変更するボタン--> <Button Text="Change" Clicked="Button_Clicked"/> </StackLayout> </ContentPage.Content> </ContentPage>
Page.xaml.cs
namespace App1 { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Page1 : ContentPage { public Page1() { InitializeComponent(); } void Button_Clicked(object sender, System.EventArgs e) { // MainPageにあるLabelにアクセスし、テキストを変更したい // エラー 'MainPage.MainLabel'はアクセスできない保護レベルになっています MainPage.MainLabel.Text = "Change Text"; } } }
Page1に遷移した後、Page1にあるボタンを押すとMainPageにあるLabel(x:Name="MainLabel")のテキストを変更するプログラムを作成したい。
ボタンを押してNavigation barの戻るボタンまたはNavigation.PopAsync(true);を使ってMainPageに戻るとLabelのテキストが変更されている流れです。
具体的なイメージとしては、Page1に設定画面があり、設定画面の内容を変更するとMainPageのラベルも内容が変更されるようなアプリを作成したいと思っています。
そもそも、仕様として難しく、設計を変えたほうが良いのでしょうか?
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/14 00:16