2015-09-03 22 views
0

login.xaml链接的TextBlock和TextBox在C#中的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> 

现在我想转由用户提供的名称,以文本块,以便它可以更改默认的名称是“玩家1个回合”

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" /> 

因此,因此我创造了两个不同的页面文件是“ login.xaml'&'MainPage.xaml'但我无法访问用户输入数据到文本块!

+2

你是如何将值传递给主页? –

+0

使用查询字符串。谷歌!谷歌!!谷歌!!! – niksofteng

+0

这是我想知道如何将值从“登录”传递给MainPage.xaml – Ethical

回答

1

您需要将值从login.xaml页面传递给MainPage.xaml。没有其他方法可以直接将值绑定到放置在不同页面上的控件。

  1. 我希望你在login.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

    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> 
    
    0

    我不知道最佳实践,但是当我想有很多信息从许多页面可访问:

    我创建了一个public class Info,一个public static class Helper并添加我的信息作为

    public static Info myInfo = new Info()

    ,并在每个页面中添加this.DataContext = Helper.my或创建一个属性信息做this.Info = Helper.myInfo并绑定它,或者你也可以做TextBlock.Text = Helper.myInfo.Player1Name

    我会添加一些代码,如果你喜欢

    相关问题