MessageDialog in Windows 8 xaml - xaml

My Code:
MessageDialog msg = new MessageDialog("Are you sure to cancel the booking?", "Confirmation");
msg.Commands.Add(new UICommand("Confirm", new UICommandInvokedHandler(CommandHandler)));
msg.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler(CommandHandler)));
msg.DefaultCommandIndex = 1;
msg.CancelCommandIndex = 1;
await msg.ShowAsync();
private async void CommandHandler(IUICommand command)
{
var commandLabel = command.Label;
switch (commandLabel)
{
case "Confirm":
CancelBookingTickets();
break;
case "Cancel":
break;
}
}
protected async void CancelBookingTickets()
{
MessageDialog msg1 = new MessageDialog("The cancellation process is complete", "Complete");
await msg1.ShowAsync();
}
I am trying to use the nested MessageDialog box in my Windows 8 xaml app but when I reach to the msg1.ShowAsync(), it fires an exception saying "Access is denied".
Can anybody help me out with this problem?

You are facing problem of multiple MessageDialog at once.
How to allow for multiple popups at once in WinRT?

Related

The video recording device is preempted by another immersive application

Am I opening the camera for taking a picture, but the user has the possibility to stop the camera if he no longer wants to capture something, so I have a close button which is intended to close the camera, so that the camera preview should be stopped.
If I open the camera, close, open again, I will get the following exception once the close button is clicked for the second time:
System.Runtime.InteropServices.COMException: 'The video recording device is preempted by another immersive application.
I do not know, how the preview camera should be stopped, more than UWP docs say here: https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/simple-camera-preview-access
The code for stopping the camera preview:
private async Task CleanupCameraAsync()
{
if (_mediaCapture != null)
{
if (_isPreviewing)
{
await _mediaCapture.StopPreviewAsync();
}
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
CameraPreviewControl.Source = null;
if (_displayRequest != null)
{
_displayRequest.RequestRelease();
}
_mediaCapture.Dispose();
});
}
}
I tried to test your code snippet on my side and it can work well. I didn't get the above exception. I tested on the emulator build 15063 and build 14393. Since your code snippet is not the completed code, so I created a minimal project for testing as follows which can work well on my side. You may test it on your side and compare with your code if something wrong with your project.
XAML
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="30">
<CaptureElement Name="PreviewControl" Stretch="Uniform"/>
<Button x:Name="btnpreview" Click="btnpreview_Click" Content="preview test"></Button>
<Button x:Name="btnstop" Click="btnstop_Click" Content="stop"></Button>
</StackPanel>
Code behind
private DeviceInformation _cameraDevice;
private MediaCapture _mediaCapture;
private InMemoryRandomAccessStream _ras;
private LowLagMediaRecording _recording;
private CameraRotationHelper _rotationHelper;
private readonly DisplayRequest _displayRequest = new DisplayRequest();
private bool _isPreviewing;
public MainPage()
{
this.InitializeComponent();
}
private async void btnstop_Click(object sender, RoutedEventArgs e)
{
if (_mediaCapture != null)
{
if (_isPreviewing)
{
await _mediaCapture.StopPreviewAsync();
_isPreviewing = false;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
PreviewControl.Source = null;
if (_displayRequest != null)
{
_displayRequest.RequestRelease();
}
_mediaCapture.Dispose();
});
}
}
}
private async void btnpreview_Click(object sender, RoutedEventArgs e)
{
try
{
_mediaCapture = new MediaCapture();
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
_cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault();
_rotationHelper = new CameraRotationHelper(_cameraDevice.EnclosureLocation);
_mediaCapture.Failed += MediaCapture_Failed;
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = _cameraDevice.Id };
await _mediaCapture.InitializeAsync(settings);
PreviewControl.Source = _mediaCapture;
_displayRequest.RequestActive();
await _mediaCapture.StartPreviewAsync();
_isPreviewing = true;
}
catch (UnauthorizedAccessException)
{
// This will be thrown if the user denied access to the camera in privacy settings
System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
}
}
If you still have issues, please provide a minimal reproduced project and the testing environment information. More details please reference the official sample.

(UWP) How to activate the "Taskbar Miniplayer" like in Groove

I use the BackgroundMediaPlayer for my App to play Audio in the Background. Now i see these buttons:
How can i activate them?
In order to make the media controls from the taskbar to work, you need to load and configure the SystemMediaTransportControls from the foreground application AND the background task. If you are doing it only from the background task, the controls will be displayed but they will remain disabled.
In your foreground application, you should have the following code:
var smtc = SystemMediaTransportControls.GetForCurrentView();
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;
And in the background task, you should have :
smtc = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;
Beware that the API to get the control instance is not the same:
SystemMediaTransportControls.GetForCurrentView()
in the foreground app and BackgroundMediaPlayer.Current.SystemMediaTransportControls in the background task.
You will have to support the button pressed event in the two (foreground + background)
That's System Media Transport Controls and you should add code to handle click event.
Here is official sample:
public MainPage()
{
this.InitializeComponent();
// Hook up app to system transport controls.
systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
systemMediaControls.ButtonPressed += SystemControls_ButtonPressed;
// Register to handle the following system transpot control buttons.
systemMediaControls.IsPlayEnabled = true;
systemMediaControls.IsPauseEnabled = true;
}
async void SystemControls_ButtonPressed(SystemMediaTransportControls sender,
SystemMediaTransportControlsButtonPressedEventArgs args)
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Play:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement.Play();
});
break;
case SystemMediaTransportControlsButton.Pause:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement.Pause();
});
break;
default:
break;
}
}

Call MessageDialog from Property Changed handler Store App

I'm trying to call a MessageDialog out of a PropertyChanged Handler. The first call is always successful, but when the Dialog gets called a second time, I get an UnauthorizedAccessException.
I've tried to wrap the call in a Dispatcher, but I got the same behavior.
Here's the code (snippet of MainPage.xaml.cs):
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
showMessage("Message", "Title");
});
}
async void showMessage(String message, String title)
{
MessageDialog dialog = new MessageDialog(message, title);
await dialog.ShowAsync();
}
Could anybody please help me with this issue?
I think your problem is that multiple property changes will cause multiple calls to display the dialog. You should only ever display one dialog at a time:
bool _isShown = false;
async void showMessage(String message, String title)
{
if (_isShown == false)
{
_isShown = true;
MessageDialog dialog = new MessageDialog(message, title);
await dialog.ShowAsync();
_isShown = false;
}
}

how to make textbox content shareable in windows 8 xaml app when user clicks share from charm settings?

I have one text textbox in my Windows8 Store app (xaml) application and I want to make it shareable when user selects Share option from Charm settings ? Is that possible to in windows 8 store xaml app ?
Yes, you need to respond to the "DataRequested" event and add the content of the textbox to the data package. Here is a code sample:
JavaScript:
// Call this during initialization
function registerForShare() {
var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
dataTransferManager.addEventListener("datarequested", shareTextHandler);
}
function shareTextHandler(e) {
var request = e.request;
request.data.properties.title = "Your title";
request.data.properties.description = "Description of what you're sharing";
request.data.setText(yourTextBox.Text);
}
C#:
// Call this during initialization
private void ShareSourceLoad()
{
var dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += DataRequested;
}
private void DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequest request = e.Request;
request.Data.Properties.Title = "Your title";
request.Data.Properties.Description = "Description of what you're sharing";
request.Data.SetText(yourTextBox.Text);
}

Error in Windows 8 xaml Modern app with thread

I have following two methods. When user clicks on start button from ui, the step geoLocator_PositionChanged in geoLocator_PositionChanged method is fired and calls the other method geoLocator_PositionChanged.But when it comes to try block while executing the first statement it throws the following error:
"The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"
private async void btnStartStop_Click_1(object sender, RoutedEventArgs e)
{
geoLocator.PositionChanged += geoLocator_PositionChanged;
}
async void geoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
MessageDialog msgdlg = null;
bool bDisplayDialog = false;
try
{
lblAltValue.Text = args.Position.Coordinate.Altitude.ToString();
}
catch
{
}
}
Any help how can I fix this issue ?
You try to access the UI-Thread from another one.
Try something like ths
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
//HERE GOES THE UI ACCESS LIKE this.textbox.text = "MY AWESOME TEXT";
});