In this particular application I am showing Video using Web Cam.When I press any button it runs Audio file using SoundPlayer.
Code to Run Audio
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
player.PlayLooping();
This code snippet freezing the UI even if I put this code in thread
still my UI is freezing
So can anyone please tell me how I can solve this problem. Thank you
Edit1:
So in above code snippet
I am declaring Sound Player object Globally
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
and calling function PlayAudio() on Button click Event
private void PlayAudio()
{
player.PlayLooping();
}
It should NOT freeze the UI, unless there is some other blocking code running. Below is a sample 'Hello World' WPF code with your code sample -
XAML -
<StackPanel>
<Button Content="Play" Height="50" Width="100" Click="Button_Click"/>
<Button Content="Test" Height="50" Width="100" Click="Button_Click_1"/>
<TextBlock x:Name="txtSample" Text="Hello"/>
</StackPanel>
xaml.cs -
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
player.PlayLooping();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
txtSample.Text = Guid.NewGuid().ToString();
}
Now click on the Play button to play the sound, now while the sound is playing click on Test button any number of times it should not freeze.
Also as per Microsoft documentation PlayLooping should not hang UI as it runs in a new thread by default.
Related
I develop chat application on Xamarin.Forms.
I need to don't hide keyboard when I press Send button and hide keyboard when I tap anywhere else.
I made it for iOS.
Can I make the same for android?
Found a very simple method to achieve this goal, you can simply place a Button on the top of an Entry like this:
<Grid VerticalOptions="End">
<Entry x:Name="MessageEntry" TextChanged="MessageEntry_TextChanged_1" />
<Button x:Name="SendButton" Text="Send" HorizontalOptions="End" Clicked="Button_Clicked" IsEnabled="False" />
</Grid>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
MessageEntry.Text = null;
}
private void MessageEntry_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (MessageEntry.Text != null)
SendButton.IsEnabled = true;
else
SendButton.IsEnabled = false;
}
Tested on Android 6.0 emulator, works fine to me, you can customize the Button to make it looks more beautiful in this view:
you can have in your PCL a scrollview element in which you add you entry element, when you tap on the entry element it will bring the keyboard and not hide it, it only hide it when tapped on a different portion of the screen. let me know if it help
I am creating an application in WPF that relies on multi-touch and, although I can receive multiple touch points, the WPF controls do not behave as expected when multiple touches occur at the same time.
I created a simple test WPF application using buttons for visualization to ensure it wasn't anything in my project causing the issue.
The XAML:
<Window x:Class="TouchSample3.MainWindow"
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:TouchSample3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid TouchDown="Grid_TouchDown">
<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="108,86,0,0" VerticalAlignment="Top" Width="70" Height="70" Click="button1_Click" TouchDown="button1_TouchDown"/>
<Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Margin="322,86,0,0" VerticalAlignment="Top" Width="70" Height="70" Click="button2_Click" TouchDown="button2_TouchDown"/>
</Grid>
</Window>
and here is my MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Button 1 clicked.");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Button 2 clicked.");
}
// TouchDown events
private void button1_TouchDown(object sender, TouchEventArgs e)
{
Console.WriteLine("Button 1 Touch Down.");
}
private void button2_TouchDown(object sender, TouchEventArgs e)
{
Console.WriteLine("Button 2 Touch Down.");
}
}
When I perform a single touch on either button it fires as expected, the TouchDown event, and Click event along with the button animation occur.
However, when I attempt to do two touches simultaneously (one finger held down, another one pressing), the TouchDown events get fired but the the Click events and the button animations do not happen.
It clearly registers the touch points but I don't understand why it doesn't perform actions/events to the WPF controls when touches happen simultaneously.
Any help or direction would be appreciated.
I think thats because the old version of Click event is not prepared well for a multi-touch wpf application. It is primary classified as a mouse event, since the TouchDown is touch event primary so it support the multi-touch feature.
I am trying to play a video in my app. The video and the xaml file are present under the same folder.
My xaml code (which is a user control)
<Grid x:Name="LayoutRoot">
<MediaElement x:Name="vid" MediaOpened="MediaElement_MediaOpened"
Source="hey.mp4" AutoPlay="True" />
</Grid>
My xaml.cs file code
public Page1()
{
InitializeComponent();
}
private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
vid.Play();
}
The video is not playing. I tried the source to be "/hey.mp4" also, but it didn't play.
What is my mistake ?
Check your URl of the video and to disable locking of the phone, you could do this,
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
I have a WPF application with some background tasks to be done that works perfectly on my laptop but it is not working fine in a tablet with Windows 7.
You click the screen, a loading image is shown while some background job is done, and at the end of the job a dialog with some info is shown. You press the button on the dialog and go back to the main screen.
In the tablet at the end when the dialog is shown you have to press the screen once to have the GUI "active" and then again to the application detect the click. Is like the ui thread gets "disabled" after the background task and needs the screen to be touch to be active again. It is quite annoying because the user has to press multiple times the button to get the action performed. This behaviour happens only in the tablet, not on a laptop...
Here is the code:
public void processTouch(Point p)
{
Task getPendingDocsTask = Task.Factory.StartNew(GetPendingDocs, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
Task afterGetPentingDocsTask = getPendingDocsTask.ContinueWith(w => AfterGetPendingDocs(), TaskScheduler.FromCurrentSynchronizationContext());
}
private void GetPendingDocs()
{
Thread.Sleep(500);
}
private void AfterGetPendingDocs()
{
mainprogresscircle.Visibility = System.Windows.Visibility.Hidden;
this.Background = (Brush)bc.ConvertFrom("#013857");
WindowDialog aDialog = new WindowDialog(this);
aDialog.ShowDialog();
}
In the dialog the function when click is:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
And the XAML of the button on the dialog
<s:SurfaceButton
Margin="-4"
BorderBrush="Transparent"
BorderThickness="0"
Width="200"
Height="75"
HorizontalAlignment="Center"
Name="OpenButton"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontFamily="Tahoma"
FontSize="20"
Content="Aceptar"
Background="#78B41E"
Foreground="White"
Click="Button_Click"
IsManipulationEnabled="True"
/>
I have tried also with BackgroundWorker and dispatcher and the behaviour is the same. Works perfectly on a regular computer but it does not response fine on a windows 7 touch device.
Any clue will be really wellcomed.
Thanks in advance,
Ivan.
I am in the process of building a metro style app and I need to know if there is a way to fire button clicks programmatically.
I have this PasswordBox and button in my xaml:
<PasswordBox IsPasswordRevealButtonEnabled="True" KeyDown="On_text_KeyDown" x:Name="SomePW" FontSize="50" KeyDown="On_text_KeyDown" Margin="0,0,0,190" Height="67" Width="363"/>
<Button Background="White" x:Name="Button_Go" Foreground="Black" Margin="20,0,0,190" Content="Go" FontSize="20" Click="Go_click" Height="67" Width="60"/>
And in my C# code this is the function that handles the key press in the PasswordBox:
private void On_text_KeyDown(object sender, RoutedEventArgs e)
{
KeyEventArgs K = (KeyEventArgs)e;
if (K.Key == Windows.System.VirtualKey.Enter)
{
//<TO-DO> Simulate Button Click Here
}
}
The problem is I can't seem to find a way to simulate the button click... Can someone help please?
Is there a problem with simply calling this or are you looking for a generic way to invoke a Click event on any button, perhaps to automate it?
Go_click(this, new RoutedEventArgs());
You can try this:
ButtonAutomationPeer peer = new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv = peer.GetPattern( PatternInterface.Invoke ) as InvokeProvider;
invokeProv.Invoke();
Another option is the following:
SomeButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Hope this helps!!