How To use NotificationChannel.SetSound under .NET MAUI - c#

I'm currently migration a project of mine from Xamarin.Forms to .NET MAUI. Thereby I'm struggling with migrating the creation of a NotificationChannel to MAUI incause of the incompatibility of Android.Net.Uri (needed for Notification.SetSound(uri, attributes);) and the System.Uri used in MAUI.
The NotificationChannel should have a specific notification sound with is located in the resources folder of the project.
Following code was used in Xamarin Forms:
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
?.SetUsage(AudioUsageKind.Alarm)
?.Build();
Android.Net.Uri alarmUri =
Android.Net.Uri.Parse(
$"{ContentResolver.SchemeAndroidResource}://{ApplicationContext?.PackageName}/{Resource.Raw.alarm}");
var alarmChannel = new NotificationChannel(id: Constants.AlarmNotificationChannel,
name: "Alarm",
importance: NotificationImportance.Max)
{
Description = "Highest priority notifications appear in this channel"
};
alarmChannel.EnableLights(true);
alarmChannel.EnableVibration(true);
alarmChannel.SetBypassDnd(true);
alarmChannel.SetShowBadge(true);
alarmChannel.SetSound(alarmUri, alarmAttributes);
notificationChannels.Add(alarmChannel);
Is it possible to set the sound of the notificationchannel even under .NET MAUI? And how can I achieve this goal?
Thanks in advance!

Your code runs in Xamarin.Android, the code that calls Android in maui has to try to use handler, the Handler system in MAUI is used to handle the implementation of native controls for different platforms, if you want to create controls, you only need to create handlers based on different platforms.
You can refer to this link: Create a custom control using handlers.
There is a method on GitHub:Plugin.LocalNotification. The local notification plugin provides a way to show local notifications from .Net MAUI. One such feature is Custom Sounds, which is supported on the Maui platform.
Plugin.LocalNotification Available on NuGet: https://www.nuget.org/packages/Plugin.LocalNotification.
Hope it could help you.

Related

How to use CoreNFC in xamarin.forms?

I'm totally new in xamarin.
I'm trying to use CoreNFC in my xamarin.forms multiplatform app.
But I don't know how to do it because i can only import CoreNfc in myapp.ios and not in myapp.
There is someone that know how to do it ?
Thanks in advance!
Core NFC belongs to the iOS Framework, so you can import and use it only in our iOS Project.
You could implement the NFC feature on each platform you want to support and use a interface and the Xamarin Dependency Service, to have access to these platform specific implementations in your shared code.
Or you look around for a plugin which did this work for you. A starting point could be NFCForms from poz1. But if I remember well it has so far only support for Android and Windows Phone.

C# trigger windows 10 media notification

How can I implement these Windows 10 "media notifications" into a C# WPF desktop application?
You are talking about the SystemMediaTransportControls class. Its usage it described on Microsoft Docs.
However, this API is designed for UWP so getting this to work in a regular desktop application like WPF takes a little more work. There are some answers here on StackOverflow that describe how to do this, but there is also some newer, official documentation from Microsoft on how to use these WinRT APIs.
Unfortunately, the System Media Transport Controls API is not directly supported in Desktop apps [5], so it gets a little more complicated, as described here [6].
In short, for .NET 5:
Install the Windows 10 SDK of a version you like (e.g. through the VS Installer);
Change the target framework to the SDK version you installed, e.g. net5.0-windows10.0.19041.0.
Use the API interops as described on [6] to get an instance.
In the case of SMTP, you'd want to use Windows.Media.SystemMediaTransportControlsInterop. This requires the handle to a window, which you can get with WindowInteropHelper in WPF.
Use the SystemMediaTransportControls instance as described on [2].

Using WPF dll's in UWP app

I'm writing a UWP app, and for some reason I'm unable to reference PresentationFramework.dll. It contains some WPF controls I want to use (specifically, System.Windows.Controls.DataGrid, but they aren't available under Universal Windows >> Extensions in the reference manager. Why is this, and how can I fix it?
TL;DR: You can't use WPF controls in UWP.
WPF and UWP are two totally different APIs with a different .NET framework. While WPF has access to the full .NET Framework, UWP has a much more limited API. If you want to read more on the different platforms and compilers, this msdn blog post is a good entry point.
You are thus unable to add any standard dlls as a reference to an UWP app. If you want to share code between WPF and UWP, you'll have to use a Portable Class Library, in which you target the platforms you need.
And as the XAML namespaces for WPF and UWP are different as well, you won't find many portable controls. So for UWP development you'll need to use UWP controls (equivalent to their WPF counterparts). Bonus tip: if you're looking for 3rd party controls, you can also look for 'winrt' controls as winrt was used for Windows 8/8.1, but it's the same technology.
I know this is an old question. Not even going to research what came out when. There is a DataGrid for UWP as part of the community toolkit:
https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/datagrid
It's really frustrating though, because it doesn't seem to do a few things that the WPF version does, and these are the few things I needed it do do.
Microsoft really needs to come up with a new naming conventions for its 114 different frameworks and platforms. Controls should be named DataGridWpf, DataGridUwp, etc.
It's super hard to search for examples of the UWP DataGrid control and find thousands of examples for the WPF version. Just my two cents. And for the record, it seems like there are about 5 active links to examples on the UWP version. Arrg.
/And for added fun, Telerik and DevExpress have UWP data grid controls, which they also call DataGrids.

How to use Native Android ImageView in a Xamarin Forms PCL?

I am new to Xamarin, but learning quickly. I have what is hopefully a straightforward question. I know that I can customize Xamarin Forms controls per platform using custom renderers, and I also know that I can use native controls that have 0-param constructors with dependency service.
my question is, how do I use a native control (specifically Android ImageView... and eventually obviously WinPhone and iOS counterparts) in my Xamarin Forms PCL app?
A very simple example would go a long way, since I'm just having trouble getting my head around how I would define an interface which would set parameters like buffering, image source, etc. on the platform-specific controls.
Thanks for your help!
With the latest update to Xamarin Forms this is a newly added feature.
You can read more about it in the blog - Embedding Native Controls into Xamarin.Forms.
Also you can find samples at Native Embedding - Adding Platform-Specific Controls to a Xamarin.Forms Layout
On Android, the following code example demonstrates how to add a TextView to a StackLayout and a ContentView:
var textView = new TextView (Forms.Context) { Text = originalText, TextSize = 14 };
stackLayout.Children.Add (textView);
contentView.Content = textView.ToView();
For this to work you have to use a Shared Project template and not PCL. For PCL you will have to use Custom Renders.

Using WinRT library in WPF application.

I have followed this article here:
http://blogs.msdn.com/b/eternalcoding/archive/2013/10/29/how-to-use-specific-winrt-api-from-desktop-apps-capturing-a-photo-using-your-webcam-into-a-wpf-app.aspx
And it all works great... but how do I get a preview in WPF?
The XAML "CaptureElement" control is not available in WPF. Is there any other way I can get a preview using the MediaCapture API?
Why would Microsoft not make this control Available?
Late reply, but in case that helps folks in the future: previewing MediaCapture in WPF is difficult but possible. Here is some code sample:
https://github.com/mmaitre314/MediaCaptureWPF
After investigating further it seems impossible to get this to work with the XAML Capture Element or any other XAML element in WPF.
I decided to port my WPF app to a Win8 application. I kept the existing back end code and referenced it using a Brokered WinRT Component. An excellent tutorial can be found here: http://devhawk.net/2014/04/25/brokered-winrt-components-step-one/ on how to create a WinRT brokered component.
Keep in mind that the app must be sideloaded and will not work for apps that you want to publish to the store.
You will have to rig up a mechanism to display the converted BitmapImage yourself, e.g., either in an Image control or as an ImageBrush on some surface. To the best of my knowledge, WinRT controls cannot be hosted in WPF.
CaptureElement was presumably not ported to WPF because it depends on WinRT APIs, and it makes little sense to introduce a dependency into WPF for an API that is only available on Windows 8+, and which is only officially supported in Windows Store apps.

Categories

Resources