UWP C# Switching frame content issues and crashes - c#

I've struggled with this setup for couple of days and found a somewhat solution but I think it not how it supposed to work.
Here is my xaml page setup:
<Page
...
<SplitView IsPaneOpen="True" DisplayMode="Inline" OpenPaneLength="300">
<SplitView.Pane>
<Grid>
<ToggleButton x:Name="Edit" IsEnabled="False" Checked="Edit_Checked" Unchecked="Edit_Unchecked"/>
</Grid>
</SplitView.Pane>
<Frame x:Name="RightFrame">
</Frame>
</SplitView>
</Page>
Code for toggle button:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage1));
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage2));
}
So basically the toggle button switches the splitview content. The SubPages are just blank pages. The problem is that app crashes when navigation occurs.
I've noticed when I put this before invoking navigation:
RightFrame.Content = null;
Thread.Sleep(1000);
Then everything works fine. So I need to clear frame content and wait for it to finish I guess. But I think it should be done automatically. Could some one explain what am I doing wrong here and how it should be done?

Please verify your code in SubPage1 and SubPage2 which might be creating the crashes, you can clear the resources when the page gets unloaded.
Alternatively, you can try below code:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage1();
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage2();
}

Related

Programmaticaly click a button with it's UserControls

Like you can see here, i got a button with Name, etc. and i got in this button a usercontrol. When i click on the button everything is alright, but when i programmaticaly click this button. The UserControls will not be performed... why?
<Button x:Name="btnHome" Style="{DynamicResource PopupButtonStyle}" MouseEnter="btnHome_MouseEnter" MouseLeave="btnHome_MouseLeave" Click="btnHome_Click">
<ctls:MenuItem GroupName="MenuItem" IndicatorBrush="{DynamicResource PrimaryBlueColor}" Icon="{DynamicResource home}" IconWidth="16" Text="Home" VerticalAlignment="Center"/>
</Button>
This is the code for clicking the button, am i wrong here?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
btnHome.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void btnHome_Click(object sender, RoutedEventArgs e)
{
}

Ignore user input when UI is busy

I will simulate my problem by taking two buttons in a sample application. In my actual case I have a UserControl along with a tabcontrol. When a button is pressed on the UserControl I am trying to get the parent Window and disabling it and perform some work on the UI for 1 minute. After the UI is free I am enabling the parent window.
My problem is when the ParentWindow is disabled and when the user clicks on the tabcontrol, as long as the UI is busy it remains unresponsive. But as soon as the UI does its work, all the user input is being taken into account and it switches tabs by itself. I want to ignore user input when the UI is busy please help. Any suggestions?
In the sample application here is the xaml
<StackPanel Orientation="Vertical">
<Button Height="20" Width="180" Click="Button_Click"
>Hello</Button>
<Button Height="20" Width="180" Click="Button_Click_1">Do Nothing</Button>
</StackPanel>
In the xaml.cs file here is code. See that I am disabling the Window but when the button is pressed i get the hello message automatically after the UI is free.
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window.GetWindow(this).IsEnabled=false;
Thread.Sleep(5000);
Window.GetWindow(this).IsEnabled=true;
}
just run your long-running-code in new thread
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Task.Run(() =>
{
try
{
this.Dispatcher.Invoke(() =>
{
Window.GetWindow(this).IsEnabled = false;
});
Thread.Sleep(5000);
}
finally
{
this.Dispatcher.Invoke(() =>
{
Window.GetWindow(this).IsEnabled = true;
});
}
});
}
expected result

Cannot play media Element programmatically

I developed an app in Windows Phone 8. I have a list with mp3 from web and a mediaelement can't play them.
This is my code
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
}
void note1_MediaOpened(object sender, RoutedEventArgs e)
{
play.Play();
}
And xaml is :
<MediaElement HorizontalAlignment="Left" Height="100" Margin="167,538,0,0" VerticalAlignment="Top" Width="100" Name="play" AutoPlay="True"/>
I have try it in xaml and play it but programmatically not. Can someone help me ?
I do not think you need to Play inside MediaOpened event. Just execute .Play after assigning Source to the MediaPlayer
Just add this to button click event
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.Play();
}

Sound don't play when a button is touched in windows phone. (Buttons navigates to next page)

I am creating application, which use Button to Navigate to another page, and I want to keep sound when user touch the button. The code works fine when there is button only, but when I use button to navigate to another page, the sound didn't play, But play when I press the Back button of Windows phone.
public void playSound()
{
MediaElement playSound1 = new MediaElement();
playSound1.Source = new Uri("/Sound/Lionsound.mp3", UriKind.Relative);
playSound1.Play();
}
void btnClassicPuzzle_Click(object sender, System.Windows.RoutedEventArgs e)
{
playSound();
NavigationService.Navigate(new Uri("/Menu/SelectPack.xaml", UriKind.Relative));
}
You are calling a function to play the sound on the button click and immediately executing the page naviagation. How compiler is processing it is after playSound1.Play(), the sound is started and it immediately calls the NavigationService. The page is changed and all the objects in the current pages are destroyed therefore, sound stops. What you have to do is navigate to the next page on MediaEnded event so it can play complete sound before navigation
<MediaElement MediaEnded="eventhandler" ../>
Referenced
Also you can check the MediaPlayer state by using MediaPlayer.State; before navigation
I think you can use a globlal MediaElement in your app.
to use a global a global MediaElement you can follow these steps.
First in your app.xaml add a ControlTemplate
<ControlTemplate x:Key="AudioContentTemplate">
<Grid x:Name="MediaElementContainer">
<!-- The media element used to play sound -->
<MediaElement Loaded="OnGlobalMediaLoaded"
Visibility="Collapsed" />
<!-- Added for the normal content -->
<Grid x:Name="ClientArea">
<ContentPresenter />
</Grid>
</Grid>
</ControlTemplate>
Second in your app.xaml.cs you have to declare the Globlal mediaElement and add methods to play and stop.
private MediaElement globalMediaElement = null;
private void OnGlobalMediaLoaded(object sender, RoutedEventArgs e)
{
if (this.globalMediaElement == null)
this.globalMediaElement = sender as MediaElement;
}
public void playMedia(Uri source)
{
this.globalMediaElement.Source = source;
this.globalMediaElement.Play();
}
public void stopMedia()
{
this.globalMediaElement.Stop();
}
Third in end of app.xaml.cs there is the initialization section and here you injected the global Mediaelement in the system with the template from the resource:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
RootFrame.Style = (Style)Resources["AudioContentTemplate"];
}
And finally you can call it in your code behind
private void btnClassicPuzzle_Click(object sender, System.Windows.RoutedEventArgs e)
{
//play the globlal MediaElement
((App)App.Current).playMedia(new Uri("/Sound/Lionsound.mp3", UriKind.Relative));
NavigationService.Navigate(new Uri("/Menu/SelectPack.xaml", UriKind.Relative));
}

Action on both button press and release

I can't get this straight: I have one button that I want one action for the press event and one action for release, I've searched everywhere and can't find an answer.
KeyDown, KeyUp or MouseLeftButtonDown doesn't work with button on windows phone 7.
First I tried combining GotFocus and Click clickmode release like this:
(As you can see I want Image1 to be shown while pressing button, and hidden when releasing the button)
xaml:
Button Click="button1_Click" ClickMode="Release" GotFocus="button1_GotFocus" Content="byt" Height="72" Margin="0,500,6,0" Name="button1" VerticalAlignment="Top"
private void button1_GotFocus(object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
}
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Visible;
}
This works only one time and could work all the time if I could loose focus from the button when releasing it (tried searching for that as well)
The other thing I tried was changing the clickmode while pressing the button, but didn't get that to work either..
something like this:
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
button1.SetValue(Button.ClickModeProperty, ClickMode.Release);
Image1.Visibility = System.Windows.Visibility.Visible;
}
(I know that the syntax is wrong somehow in the second one)
Would be grateful for help!
MouseLeftButtonDown / MouseLeftButtonUp do work on WP7. Obviously not named the best, but they do work on the device.
<TextBlock x:Name="ApplicationTitle" MouseLeftButtonDown="ApplicationTitle_MouseLeftButtonDown" MouseLeftButtonUp="ApplicationTitle_MouseLeftButtonUp"
Style="{StaticResource PhoneTextNormalStyle}"
Text="MY APPLICATION" />
You'll see Down gets fired, and then immediately Up.
private void ApplicationTitle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
private void ApplicationTitle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
Perhaps all you need is MouseLeftButtonDown/Up.
I used
Click="{x:Bind UpArrow_Click}"
ClickMode="Press"
PointerCaptureLost="Button_Release"
on a button. When button is pressed UpArrow_Click event occurs... when button is released Button_Release event occurs

Categories