MediaElement doesn't play sound (Minimum Viable Example) - c#

I'm developing a Windows App (Windows 8.1). In my xaml I have a MediaElement linked to a .wav file in the /Assets folder. I'd like to play the sound when I click on a button, but I can't achieve it. This is a minimum viable example that reproduces the issue. Can you help me?
This is the XAML file:
<Page
x:Class="TrySoundsDesktop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TrySoundsDesktop"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Vertical">
<TextBlock FontSize="50" HorizontalAlignment="Center">
TRY PLAY SOUND
</TextBlock>
<MediaElement x:Name="clickSound"
Loaded="clickSound_Loaded"
MediaFailed="clickSound_MediaFailed"
MediaOpened="clickSound_MediaOpened"
MediaEnded="clickSound_MediaEnded"
AudioCategory="ForegroundOnlyMedia"
Height="100"
Width="100"
Opacity="1"
Source="/Assets/applause.wav"
AutoPlay="False"
/>
<Button Click="Button_Click" Content="CLICK TO PLAY SOUND"/>
</StackPanel>
</Page>
And this is the code behind:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace TrySoundsDesktop
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
clickSound.Play(); //The sound doesn't play
}
private void clickSound_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
//Not fired
}
private void clickSound_MediaOpened(object sender, RoutedEventArgs e)
{
//Not fired
}
private void clickSound_MediaEnded(object sender, RoutedEventArgs e)
{
//Not fired
}
private void clickSound_Loaded(object sender, RoutedEventArgs e)
{
//THIS METHOD FIRES
}
}
}
As you can see it's a very basic example. But it doesn't work. Thank you in advance.

Related

How can I make button visible when Registration was successful?

So I have a simple Starting Window, where buttons like: "Login", "Register", "Exit" and finally "Start Program" are.
The "Start Program"-button is by default invisible.
The "Login"-button doesn't work yet so forget about it.
The "Exit"-button closes the program.
The "Register"-button opens an new Window, where you can type in your username and password. This is then saved in a table connected with a SQL-Database.
I have a public bool that is called "LogIn" where as default is set to false. When I successfully register I set it to true. In my Startup.xaml I then check if the bool "LogIn" is true, if it is true it should set the visibility of my "Start Program"-button to visible. But unfortunately it doesn't work.
Thanks in advance for your help.
My Code:
My Startup.xaml:
<Window x:Class="MiiUse.Startup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MiiUse"
mc:Ignorable="d"
Title="Welcome To MiiUse" Height="500" Width="850"
Style="{StaticResource Startup}"
StateChanged="MaximizeWindow" ResizeMode="NoResize">
<Grid>
<Button Style="{StaticResource RoundButton}" Content="Start Program" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Start" x:Name="StartButton" Visibility="Hidden"/>
<Button Style="{StaticResource RoundButton}" Content="Exit" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="Button_Exit" />
<Button Style="{StaticResource RoundButton}" Content="Register" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,16,0" Click="Button_Register"/>
<Button Style="{StaticResource RoundButton}" Content="Login" HorizontalAlignment="Right" Margin="0,0,77,0" VerticalAlignment="Top" RenderTransformOrigin="0.379,0.002" Click="Button_Login"/>
</Grid>
</Window>
How it looks like:
My Startup.xaml.cs:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MiiUse
{
/// <summary>
/// Interaction logic for Startup.xaml
/// </summary>
public partial class Startup : Window
{
public Startup()
{
InitializeComponent();
Registration registration = new Registration();
if (registration.LogIn == true)
{
StartButton.Visibility = Visibility.Visible;
}
}
private void Button_Start(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow();
this.Close();
mainWindow.Show();
}
private void Button_Exit(object sender, RoutedEventArgs e)
{
this.Close();
}
private void MaximizeWindow(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
}
private void Button_Register(object sender, RoutedEventArgs e)
{
Registration registration = new Registration();
registration.Show();
}
private void Button_Login(object sender, RoutedEventArgs e)
{
}
}
}
My Registration.xaml:
<Window x:Class="MiiUse.Registration"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MiiUse"
mc:Ignorable="d"
Title="Registration" Height="320" Width="370">
<Grid>
<Label Content="Enter your Username:" HorizontalAlignment="Left" Margin="44,44,0,0" VerticalAlignment="Top"/>
<Label Content="Enter your Password:" HorizontalAlignment="Left" Margin="44,98,0,0" VerticalAlignment="Top" />
<Button Content="Submit" HorizontalAlignment="Left" Margin="44,245,0,0" VerticalAlignment="Top" Click="Submit"/>
<Button Content="Cancel" HorizontalAlignment="Left" Margin="124,245,0,0" VerticalAlignment="Top" Click="Cancel"/>
<TextBox x:Name="Username" HorizontalAlignment="Left" Margin="44,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<PasswordBox x:Name="Password" HorizontalAlignment="Left" Margin="44,129,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
How it looks like:
My Registration.xaml.cs:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MiiUse
{
/// <summary>
/// Interaction logic for Registration.xaml
/// </summary>
public partial class Registration : Window
{
public Registration()
{
InitializeComponent();
}
public bool LogIn = false;
private void Submit(object sender, RoutedEventArgs e)
{
string username = Username.Text;
string password = Password.Password;
if(Password.Password.Length == 0)
{
MessageBox.Show("Password can't be empty!", "Invalid Input!", MessageBoxButton.OK, MessageBoxImage.Error);
Password.Focus();
}else if(Username.Text.Length == 0)
{
MessageBox.Show("Username can't be empty!", "Invalid Input!", MessageBoxButton.OK, MessageBoxImage.Error);
Username.Focus();
}
else
{
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connection_String))
{
using SqlCommand sqlCommandGetTemplatesAndDrafts = new SqlCommand(#$"
Insert into tbl_Users (Username, Password) values('{username}','{password}')", connection);
connection.Open();
sqlCommandGetTemplatesAndDrafts.ExecuteNonQuery();
connection.Close();
}
MessageBox.Show("You were successfully registered, and automatically logged in!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
Close();
LogIn = true;
}
}
private void Cancel(object sender, RoutedEventArgs e)
{
Close();
}
}
}
You are not setting the Visibility property after the Registration window has been opened.
You could display the Registration window as a modal:
private void Button_Register(object sender, RoutedEventArgs e)
{
Registration registration = new Registration();
registration.ShowDialog();
if (registration.LogIn == true)
{
StartButton.Visibility = registration.LogIn ? Visibility.Visible : Visibility.Hidden;
}
}
Also set LogIn before you close the window in Submit:
...
LogIn = true;
Close();
Another option would be to inject Registration with a reference to MainWindow:
private readonly MainWindow _mainWindow;
public Registration(MainWindow mainWindow)
{
_mainWindow = mainWindow;
InitializeComponent();
}
...
private void Submit(object sender, RoutedEventArgs e)
{
...
_mainWindow.StartButton.Visibility = Visibility.Visible;
Close();
...
MainWindow:
private void Button_Register(object sender, RoutedEventArgs e)
{
Registration registration = new Registration(this);
registration.Show();
}
Buf if you are serious about WPF and XAML, you should learn [MVVM].
convert your "LogIn" from bool to "Visibility" so instead of true or false we can mention Visibility.visible or Visibility.hidden. as follows:
public Visibility LogIn = Visibility.Hidden;
finally, bind your startup button visibility to this LogIn variable
So on successful completion of SQL :
LogIn = Visibility.visible ;
finally binding:
<Button Style="{StaticResource RoundButton}" Content="Start Program" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Start" x:Name="StartButton" Visibility="{binding LogIn}"/>
but this LogIn should either be in code behind or View model
but I think the best way to do it is to create a public class called helper and store these connection variables in it so that they are accessed throughout your application
create propoerty :
private Visibility _logIn;
public Visibility LogIn
{
get => _logIn;
set
{
_logIn= value;
OnPropertyChanged();
}
}
and some wherer in your application initialise it to hidden

UserControl Data Binding retrieve value

with this simple code:
MainWindows:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace itemcontrole_lesson
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class TodoItem
{
public string Username { get; set; }
public int Completion { get; set; }
}
public partial class MainWindow : Window
{
List<TodoItem> items = new List<TodoItem>();
public MainWindow()
{
InitializeComponent();
items.Add(new TodoItem() { Username = "Eric", Completion = 45 });
items.Add(new TodoItem() { Username = "Maxwell", Completion = 80 });
items.Add(new TodoItem() { Username = "Sarah", Completion = 60 });
icTodoList.ItemsSource = items;
}
}
Mainwindows XAML:
<Window x:Class="itemcontrole_lesson.MainWindow"
xmlns:local="clr-namespace:itemcontrole_lesson"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ItemsControl x:Name="icTodoList">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:UserControl1}">
<local:UserControl1 />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
then a simple UserControle:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace itemcontrole_lesson
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
//String CurentUserName = ????????
//Int Progress = ????????
}
private void Button_Click(object sender, RoutedEventArgs e)
{
///hows to remove the user from items in main windows??
}
}
}
UserControle XAML
<UserControl x:Class="itemcontrole_lesson.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="54.181" Width="399.331">
<Grid Margin="0,0,-155,0">
<Label Content="{Binding Username}" HorizontalAlignment="Left" Margin="23,23,0,0" VerticalAlignment="Top"/>
<Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0" VerticalAlignment="Top" Width="119" Click="Button_Click"/>
<ProgressBar HorizontalAlignment="Left" Value="{Binding Completion}" Height="10" Margin="204,23,0,0" VerticalAlignment="Top" Width="154"/>
</Grid>
</UserControl>
Pressing F5 everything will bind ok if you test.
But! how I'm supposed to retrieve my variable value in my usercontrole code?
see where I put comment in UC.
1- I need at least to find a way to remove this control from UI and from Items list?.
2-I'd like access username in my control and set it into a var
any suggestion?
Solution 1:
Use Tag property of button like below:
<Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0"
VerticalAlignment="Top" Width="119" Click="Button_Click" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
Event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
List<object> list = (button.Tag as ItemsControl).ItemsSource.OfType<TodoItem>().ToList<object>();
list.Remove(button.DataContext);
(button.Tag as ItemsControl).ItemsSource = list;
}
Solution 2:
More elegant solution:
Create this Style in your MainWindow:
<Window.Resources>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="Button_Click"/>
</Style>
</Window.Resources>
So now the Handler of any Button Click event is in the MainWindow.xaml.cs.
Then change handler definition like below:
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
items.Remove(button.DataContext as TodoItem);
icTodoList.ItemsSource = null;
icTodoList.ItemsSource = items;
}

Data Binding in MVVM WPF

I have little knowledge in MVVM pattern. My problem is the data does not get binded to the xaml controls, no error is there. The progressbar is still at 0.
HomepageViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using CSMS_MVVM.Models;
namespace CSMS_MVVM.ViewModels
{
class HomepageViewModel : INotifyPropertyChanged
{
BackgroundWorker _worker;
public int _progress=20;
public int Progress
{
get { return _progress; }
set {
_progress = value;
OnPropertyChanged(new PropertyChangedEventArgs("Progress"));
}
}
public void startBackgroundProcess()
{
_worker = new BackgroundWorker();
_worker.DoWork += new DoWorkEventHandler(worker_DoWork);
_worker.ProgressChanged += worker_Progress_Changed;
_worker.RunWorkerAsync();
}
private void worker_Progress_Changed(object sender, ProgressChangedEventArgs e)
{
Progress = e.ProgressPercentage;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Progress = 20;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
#endregion
}
}
Homepage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSMS_MVVM.ViewModels;
namespace CSMS_MVVM.Views
{
/// <summary>
/// Interaction logic for Homepage.xaml
/// </summary>
public partial class Homepage : Page
{
HomepageViewModel hvm;
public Homepage()
{
InitializeComponent();
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
hvm = new HomepageViewModel();
hvm.startBackgroundProcess();
}
}
}
Homepage.xaml
<Page x:Class="CSMS_MVVM.Views.Homepage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CSMS_MVVM.ViewModels"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="1000"
Title="Homepage" Loaded="Page_Loaded_1" Name="main">
<ProgressBar Name="pbStatus" Value="{Binding Path=Progress}" HorizontalAlignment="Center" Height="20" Margin="0,582,0,0" VerticalAlignment="Top" Width="300">
-----
<ProgressBar.Effect>
<DropShadowEffect Color="#FFB6B6B6" ShadowDepth="3" BlurRadius="15" Direction="310"/>
</ProgressBar.Effect>
</ProgressBar>
<TextBlock Text="{Binding Progress}" Margin="0,582,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" />
<Label Content="Loading ..." HorizontalAlignment="Center" Margin="0,607,0,0" VerticalAlignment="Top"/>
------
</Page>
Am i doing the correct coding? I searched the internet for an example but i did not understand them.
Thanks!
All bindings you set will bind to the DataContext if nothing else is specified.
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
hvm = new HomepageViewModel();
hvm.startBackgroundProcess();
this.DataContext = hvm;
}
Once you have your data context set to your viewmodel instance, it should work.
You can set the DataContext of the page in the code behind.
Another option is to create the ViewModel as a XAML resource, and set the data context of the child element via Data Binding:
<Page x:Class="CSMS_MVVM.Views.Homepage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CSMS_MVVM.ViewModels"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="1000"
Title="Homepage" Loaded="Page_Loaded_1" Name="main">
<Page.Resources>
<!-- This creates the instance of the HomepageViewModel -->
<local:HomepageViewModel x:Key="HomepageViewModel" />
</Page.Resources>
<ProgressBar DataContext="{StaticResource HomepageViewModel}"
Value="{Binding Path=Progress}">
public Homepage()
{
InitializeComponent();
hvm = new HomepageViewModel();
this.DataContext=hvm;
}
Create view model object inside constructor.

Using an object in code, which is instantiated in xaml

I am trying to experiment making simple GUI application.
In it I have made a simple class called consle. I am using a button click to trigger an event Button_Click_1. This should in turn call the function wrStr which changes the value of variable change internally. This changed value should reflect in the GUI because the variable change is also bound in the XAML code.
The problem is, I do not know how to access the consle type object instantiated in XAML code. If I get the name of the object instantiated in XAML I could just say <NameOfObject>.wrStr() inside the Button_Click_1 function.
The XAML code is given below:
<Window x:Class="TryBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TryBinding"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Set44" HorizontalAlignment="Left" Margin="244,37,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<TextBlock HorizontalAlignment="Left" Margin="339,76,0,0" TextWrapping="Wrap" Text="{Binding Source=consle, Path=change, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"/>
</Grid>
The cs code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TryBinding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
}
}
public class consle
{
public string mainstr {get; set;}
public int change { get; set; }
public consle()
{
}
public void wrStr()
{
change = 44;
}
}
}

How do I focus a TextBox in an AppBar in a C# Metro application?

I have the following code:
<Page.BottomAppBar>
<AppBar x:Name="MyAppBar" Height="32" IsOpen="True" Opened="MyAppBar_Opened">
<TextBox x:Name="SearchBar" Grid.Row="2" KeyDown="SearchBar_KeyDown"/>
</AppBar>
</Page.BottomAppBar>
The problem is that it doesn't have focus when the application opens, it also doesn't have focus when I right click twice to close and open it again.
I currently have the following code in both MyAppBar_Opened and OnNavigatedTo:
if (MyAppBar == null)
return;
MyAppBar.Focus(Windows.UI.Xaml.FocusState.Keyboard);
if (SearchBar == null)
return;
SearchBar.Focus(Windows.UI.Xaml.FocusState.Keyboard);
But that doesn't seem to have any effect, other enum values like Mouse or Programmatic don't work either. What am I doing wrong that results in no activation?
I know an alternative that used to work before, but Metro FocusManager has no SetFocusedElement?
This works for me:
XAML:
<Page
x:Class="Application5.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Application5"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
<AppBar
x:Name="MyAppBar"
Height="32"
IsOpen="True"
Opened="MyAppBar_Opened"
Loaded="MyAppBar_Loaded"
VerticalAlignment="Bottom">
<TextBox
x:Name="SearchBar"
HorizontalAlignment="Stretch" />
</AppBar>
</Grid>
</Page>
Code behind:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace Application5
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void MyAppBar_Opened(object sender, object e)
{
if (SearchBar != null)
SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
private void MyAppBar_Loaded(object sender, RoutedEventArgs e)
{
if (SearchBar != null)
SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
}
}

Categories

Resources