Reputation: 1
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>
Now i want to transfer the name provided by user to textblock, so that it can change the default name that is "player 1 turn"
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" />
So therefore i had created two different pages one is 'login.xaml' & 'MainPage.xaml' But i'm not able to access the user input data to textblock!
Upvotes: 0
Views: 534
Reputation: 485
MVVM solution:
ViewModel:
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>
Upvotes: 1
Reputation: 3998
I don't know about best practices but when i wanna have many info accessible from many pages:
I create a public class Info
, a public static class Helper
and I add my Info as
public static Info myInfo = new Info()
and in each page addthis.DataContext = Helper.my
or create a property Info do this.Info = Helper.myInfo
and bind it or you can also do TextBlock.Text = Helper.myInfo.Player1Name
I ll add some code if you like
Upvotes: 0
Reputation:
You need to pass the value from login.xaml page to MainPage.xaml. There is no other way you can directly bind values to the control placed on different pages.
Send(login.xaml) :
string s = player1.Text;
this.Frame.Navigate(typeof(MainPage),s );
Receive(MainPage.xaml):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string s = Convert.ToString(e.Parameter);
playerTurn.Text = s;
}
Upvotes: 1