2015-09-03 25 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

    我會添加一些代碼,如果你喜歡

    相關問題