login.xaml
<TextBox x:Name="player1" HorizontalAlignment="Left" Margin="544,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="44" Width="280" CacheMode="BitmapCache" FontFamily="Century Schoolbook" FontSize="26">
<TextBox.Foreground>
<SolidColorBrush Color="White" />
</TextBox.Foreground>
<TextBox.Background>
<SolidColorBrush Color="#FF1EA600" Opacity="0.645"/>
</TextBox.Background>
</TextBox>
现在我想将用户提供的名称转移到文本块,以便它可以更改默认名称&#34;播放器1转&#34;
MainPage.xaml中
<TextBlock x:Name="playerTurn" TextWrapping="Wrap" Text="Player 1 Turn" VerticalAlignment="Top" Height="70" FontSize="50"
Foreground="Cyan" TextAlignment="Center" FontFamily="Century Gothic" />
因此,我创建了两个不同的页面,一个是&#39; login.xaml&#39; &安培; &#39; MainPage.xaml中&#39;但是我无法访问用户输入数据到文本块!
答案 0 :(得分:1)
您需要将login.xaml页面中的值传递给MainPage.xaml。没有其他方法可以直接将值绑定到放置在不同页面上的控件。
发送(login.xaml):
string s = player1.Text;
this.Frame.Navigate(typeof(MainPage),s );
接收(MainPage.xaml中):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string s = Convert.ToString(e.Parameter);
playerTurn.Text = s;
}
答案 1 :(得分:1)
MVVM解决方案:
<强>视图模型:强>
public string PlayerName { get; set; }
public ICommand LoginCommand { get; private set; }
private void OnLogin(object obj)
{
//STORE PlayerName in Global Context and after navigate to MainPage, read it.
GlobalContext.PlayerName = this.PlayerName;
this.Frame.Navigate(typeof(MainPage));
}
private bool CanLogin(object arg)
{
return string.IsNullOrEmpty(PlayerName) ? false : true;
}
public CONSTRUCTOR()
{
LoginCommand = new DelegateCommand<object>(OnLogin, CanLogin);
}
<强>的Xaml:强>
<TextBox Width="100" Height="20" Text="{Binding PlayerName, Mode=TwoWay}"></TextBox>
<Button Content="Login" Command="{Binding LoginCommand}"></Button>
答案 2 :(得分:0)
我不了解最佳做法,但当我想从许多页面访问许多信息时:
我创建了一个public class Info
,一个public static class Helper
,我将我的信息添加为
public static Info myInfo = new Info()
并在每个页面中添加this.DataContext = Helper.my
或创建一个属性Info do this.Info = Helper.myInfo
并绑定它,或者您也可以TextBlock.Text = Helper.myInfo.Player1Name
如果你愿意,我会添加一些代码