WPF Task on Touch device not back properly to UI Thread - c#

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.

Related

Playing Audio file freezing UI using C#

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.

Icon not showing when asked to

My app is blocking when i want to navigate to certain view, so i want to show a load icon. The problem is the icon never shows when expected. The view changes and i never see the icon, but if i go back the icon is there.
I tried using an async task to do the navigation, but the navigation doesn't work in a task, i guess.
Any suggestions or ideas?
XAML Code:
<UserControl xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:fa="http://schemas.fontawesome.io/icons/">
<Grid>
<Listbox ItemsSource={Binding ItemsList}>
</Listbox>
<Canvas Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="-550, 180, 0, 0">
<fa:ImageAwesome Visibility="{Binding LoadingIcon, UpdateSourceTrigger=PropertyChanged}"
Icon="Spinner"
Spin="True"
Canvas.Left="56"
Canvas.Top="-17" />
</Canvas>
</Grid>
<UserControl.InputBindings>
<KeyBinding Key="Enter" Command="{Binding NavigateToMainMenuCommand}"/>
</UserControl.InputBindings>
</UserControl>
ViewModel:
public class LoginViewModel: ViewModelBase, INotifyPropertyChanged, INavigationAware
{
public InicioContableViewModel(IRegionManager regionManager,
IEventAggregator eventAggregator)
{
_regionManager = regionManager;
_eventAggregator = eventAggregator;
NavigateToMainMenuCommand = new DelegateCommand(NavigateToMainMenu);
LoadingIcon = Visibility.Hidden;
}
public DelegateCommand NavigateToMainMenuCommand { get; private set; }
private Visibility loadingIcon;
public Visibility LoadingIcon
{
get
{
return loadingIcon;
}
set
{
SetProperty(ref loadingIcon, value, nameof(LoadingIcon));
NotifyPropertyChanged(nameof(LoadingIcon));
}
}
private void NavigateToMainMenu()
{
LoadingIcon = Visibility.Visible;
string mainUri = nameof(SomeRoute.MainMenu);
_regionManager.RequestNavigate(Regions.MainRegion, mainUri);
}
}
In your Icon's properties, try to set Build Action to Resource.
WPF uses the UI thread to do things like handle mouse movement, change icons when you tell it to and to switch out one view for another. All one thread.
When that thread is busy doing something then nothing is likely to change in your UI until it's finished.
Hence, if you tell it to make something visible AND navigate, you can well find all that happens visually is the navigation. Because it has no time to show your icon.
Try making your method async and giving it a little time:
private async void NavigateToMainMenu()
{
LoadingIcon = Visibility.Visible;
await Task.Delay(60);
string mainUri = nameof(SomeRoute.MainMenu);
_regionManager.RequestNavigate(Regions.MainRegion, mainUri);
}
That task.delay should give it enough time to redraw a bit of ui.
If you need a loading icon and the thing you are navigating to is blocking then I suspect you have other problems. It's likely that whatever this new view is doing to initialise should be asynchronous. Maybe with data access etc on background threads.
You may most likely make use of the functions:- InvalidateVisual() or UpdateLayout(). Those will force to redraw and may resolve your icon visibility issues.
My app is blocking when i want to navigate to certain view, so i want to show a load icon.
If the app is blocking, you cannot show the icon, because, you know, the app is blocked. You have to remove the blocking, that is, make navigation itself quick, and do whatever initialization asynchronously. The view you're navigating to may need stuff from a database or connection to a usb device, but it won't need it immediately. You can still query the database or whatever in the background after the view is shown.
show wait indicator-> navigate -> creates view model -> starts initializion task -> initialization completes -> hide wait indicator

Stop screen capture on UWP app in TaskView of Windows 10

I stop screen capturing on my UWP app by
ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
However, the app still shows its preview on Taskview of Windows 10
Can someone let me know how to disable the app's preview on Task view
This is the current TaskView
and this is how I need it
Capturing an app's thumbnail looks like a system behavior, and I don't know how to disable it.
But since the capture only happens when the user presses Alt+Tab or clicks the task switch button on the task bar, there is a chance to cover the app with an overlay before system takes a screen capture of the app.
First add an opaque overlay, and set its initial visibility as Collapsed.
<Grid>
<TextBlock FontSize="50" Text="Your controls here!" />
<Grid Background="Black" x:Name="overlay" Visibility="Collapsed" />
</Grid>
Then register a handler for app window's Activated event,
Window.Current.Activated += Current_Activated;
Display/Hide the overlay when window is deactivated/activated,
private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
{
overlay.Visibility = Visibility.Visible;
}
else
{
overlay.Visibility = Visibility.Collapsed;
}
}

WPF Why button's background is blinking after press?

I created a UserControl, and added a Button inside it removing the Background and Text properties:
<Button x:Name="Button"
HorizontalAlignment="Center"
Height="40"
VerticalAlignment="Center"
Width="40"
RenderTransformOrigin="0,-2"
Margin="0,0,0,0"
BorderBrush="{x:Null}"
Click="Button_Click"
Background="{x:Null}"/>
I also hadled the Button Click event as below:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button.Content = new cross();
}
The above code fills the Button content with another UserControl which is a simple cross pic.
I have placed the UserControl with the Button into a MainWindow app and after pressing Button, it starts blinking - background is fluently changing between two colours. Beside my functionality from code works good. I just don't know how to get rid of that blinking background.
Before click:
After click:
You could set Focusable="False" at your Button to achive this.
But you should read about the Focusableproperty in the MSDN to check if it's ok for you. I guess you can't focus the Buttonusing the tab key anymore. But maybe that's not a problem for you.

How to show ProgressBar control while a page is loading?

I have WP7 application with several pages. When a user navigates through them it takes some time to load information. So before showing him/her the page I'd like to show “Loading…” message.
I created progress bar and placed it on the page:
<StackPanel x:Name="progressBarMain" Grid.Row="1" Grid.ColumnSpan="2" Visibility="Collapsed">
<TextBlock Text="Loading..." HorizontalAlignment="Center" VerticalAlignment="Center" />
<ProgressBar Margin="10" Height="30" IsIndeterminate="True"/>
</StackPanel>
And I'm trying to show it (and hide everything else) in the page's constructor, and hide it (and show everything else) in Page.Loaded handler.
public SomePage()
{
InitializeComponent();
Loaded +=OnSomePageLoaded;
progressBarMain.Visibility = Visibility.Visible;
ContentPanel.Visibility = Visibility.Collapsed;
}
private void OnSomePageLoaded(object sender, RoutedEventArgs e)
{
progressBarMain.Visibility = Visibility.Collapsed;
ContentPanel.Visibility = Visibility.Visible;
}
But it doesn’t' work.
Any ideas? Thank you!
Alex demonstrates showing a progress bar while the app is starting up here.
Creating a Splash Screen with a progress bar for WP7 applications. - Alex Yakhnin's Blog
Although you cannot directly manipulate the splash screen (which is static), you can display a popup (by the way, that is exactly what is done in Alex's solution) and wait for a background (read: loading) operation to complete.
Yes, you'll need to create a separate XAML Pop-up page that is loaded when the app boots up. For more details on Splash Screens, there is a code sample from MSDN:
"Code Sample for Splash Screen"

Categories