I am new to Xamarin and Xamarin Forms. I am using the Sample form MediaManager https://github.com/martijn00/XamarinMediaManager. It works with the sample url, but I can't get it to work with a local resource for either iOS or Android.
I've only tested this in iOS so far but place the file in your Resources directory.
Then use this code to play the file.
iOS
await CrossMediaManager.Current.Play("myfile.mp3", MediaFileType.Audio, ResourceAvailability.Local);
Android:
Place file in Resource/raw folder.
Do not add file extension to name.
await CrossMediaManager.Current.Play("android.resource://[PackageName]/raw/myfile", MediaFileType.Audio, ResourceAvailability.Local);
Related
I want to use Remote Config in a application Xamarin.Forms.
I use a blog - Firebase Remote Config in Xamarin Forms
I have a question about the section: Setup Firebase Remote Config.
Quote
We can setup Firebase Remote Config through the Firebase portal https://console.firebase.google.com/project/Your-Firebase-Project/config and it is an easy process because the portal GUI is nice and friendly. To do it, we add a new parameter key and the default value and of course it can be a json (the GUI has a tool to validate the format of the json content). In the example case the key will be Features and the default value a json:
{
"ShowPlayerDetail": false
}
In this case, we setup a key called ShowPlayerDetail to show or not the player detail.
Question:
Where is the JSON file referred to in this section?
Note.
I am currently in a project.
Update-1
Update-2
Update-3
Update-4
Update-5
I have completed the following steps.
Did I do the right thing?
Do my actions correspond to what is described in the blog?
Or do I need to perform Publish changes? (see Pic-4. Result)
Pic-1
Pic-2
Pic-3
Pic-4. Result
You're using an existing Firebase project but there are no apps in your project, please see the Update-4 screenshot .
You could click the iOS or Android icon to add app to your Firebase project, and make sure the Apple bundle ID matches the Bundle identifier in info.plist of your Xamarin.iOS project, the Android package name matches the packagename in AndroidManifest.xml of your Xamarin.Android project. After registering the app, you can download the google-services.json file for Android and the GoogleService-Info.plist file for iOS.
In iOS 13, UIScene is used. Codes in AppDelegate.cs must be moved into SceneDelegate.cs to support multiple windows of the same app in Split View.
For Xamarin.Forms, AppDelegate.cs uses LoadApplication (new App()) to launch an instance of App.cs in the Xamarin.Forms. LoadApplication is found in Xamarin.Forms.Platform.iOS.FormsApplicationDelegate.
What is the equivalent in SceneDelegate.cs to launch an instance of App.cs in the Xamarin.Forms?
From the app lifecycle of xamrin forms :
iOS – Main method > AppDelegate > App > ContentPage .
Android – MainActivity > App > ContentPage
We will see that Main method invoke App class before , if need a instance of App from iOS , generally will try as follow :
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(app)); // pass app to Forms
return base.FinishedLaunching(app, options);
}
However ,Forms can not using UIKit (error screenshot).
In xamarin forms , there is DependencyService to load navtive method . Therefore, suggest that using DependecyService to call app from iOS native AppDelegate.cs .
About using SceneDelegate.cs in Xamarin Forms , there is no SceneDelegate.cs file in iOS solution now . I will check that whether be possible in Xamarin Forms .
==================================Update==============================
If want to deal with a universal link in AppDelegate.cs , you need to do something in continueUserActivity method as follow :
public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
//return base.ContinueUserActivity(application, userActivity, completionHandler);
if(userActivity.ActivityType == NSUserActivityType.BrowsingWeb)
{
NSUrl url = userActivity.WebPageUrl;
// other code
}
return true;
}
==================================Update===============================
Finally , found that it is possible to add SceneDelegate to a Xamarin Forms project. A new Xamarin Forms project does not come with the necessary SceneDelegate.cs or .storyboard files, so these need to be added. After adding these files, the info.plist needs to be updated with the UIApplicationSceneManifest key, which will contain more needed keys.
The additions to info.plist are shown here: https://learn.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration (just UIApplicationSceneManifest and everything under)
The two things to note are that:
The sample has issues with navigation working properly when having multiple windows of the app running.
This is not an official sample, as Xamarin.Forms does not currently offer official support for using mutiple Scenes with an iOS application.
The unofficial Xamarin.Forms sample is here:https://www.dropbox.com/s/sdxq5me7vcdmuf9/XamFormsiOSMultiWindow.rar?dl=0
It seems this issue has been resolved in MAUI. Just inherit from MauiUISceneDelegate for your SceneDelegate class and the framework will take car of the rest:
[Register("SceneDelegate")]
public class SceneDelegate : MauiUISceneDelegate
{
}
And then in your info.plist file:
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>__MAUI_DEFAULT_SCENE_CONFIGURATION__</string>
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
I wish there was an equally clean solution for Xamarin.Forms as we have not migrated our app to MAUI yet.
Hope this helps!
*Update:
Junior Jiang's solution did work for me, but I had to make a few changes to get it to work:
Uncomment the following lines in SceneDelegate.cs:
//var ad = new AppDelegate();
//ad.GetUI();
...
//Window.RootViewController = ad.Window.RootViewController;
Then add the following line after the uncommented code in SceneDelegate.cs
Window.MakeKeyAndVisible();
I made a few additional changes that are not specifically required, but probably a bit better in terms of resource usage, such as rather resolving appDeleggate as follows:
var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
And instead returning the created Xamarin.Forms app in GetUI method (in AppDelegate) to be used in SceneDelegate, instead of creating another one in SceneDelegate.
One limitation that I did notice is that I could no longer see iOS alert messages, so I used a custom syncfusion popup instead.
I have a webview inside my Xamarin Forms application which opens report downloaded from server in html format. Today I uploaded my archived application to app store and get information about ITMS-90809: UIWebView API Deprecation.
The problem is that I have no idea how to replace UIWebView with WKWebView in cross platform application. I've tried several solutions:
1) Creating Custom renderer for WebView - but this is no solution really because I need to inherit my new class by WebView superclass. So Apple warn me again with the same message.
2) Creating dependency service and use custom renderer in ios project only. But this is the same problem - there is still WebView reference in shared project.
So, is there any way to inject WKWebView directly to StackLayout in iOS? I know that WKWebView is iOS only and StackLayout is Xamarin.Forms component but really I don't have any idea how to do this.
Can anyone help me?
There is an issue in Github you can follow: Deprecated API Usage UIWebView
WebViewRenderer in iOS implements UIWebView, and the file stays even if you switch over the WkWebViewRenderer. That's why you get the error message.
Xamarin team is working on it and it will fixed sometime in the future.
Also, there is an Apple statement says:
The App Store will no longer accept new apps using UIWebView as of
April 2020 and app updates using UIWebView as of December 2020.
So you can still submit app with UIWebView right now and the error messgae is just a warning.
Relevant links: Make WkWebViewRenderer default for iOS WebView
I am trying to embed an image in Xamarin and was expecting to see a resource Id appear in the properties of the image. It seems to be a problem that I cannot find an answer for. I have updated VS, JDK to 64 bit, updated Xamarin, cleaned rebuilt, restarted. I'm using the latest API (API 27 8.1)
I have checked the Xamarin forum and the VS forums and there is no answer available. In my code-behind I am writing the following:
bgImage.Source = ImageSource.FromResource("FoodSnapApps.Images.steps.jpg");
Obviously it wont work as the resource has no reference.
As always, any help will be greatly appreciated.
As stated here, in Xamarin forms local images must be loaded in the platform specific projects:
Image files can be added to each application project and referenced from Xamarin.Forms shared code. To use a single image across all apps, the same filename must be used on every platform, and it should be a valid Android resource name (ie. only lowercase letters, numerals, the underscore, and the period are allowed).
iOS - The preferred way to manage and support images since iOS 9 is to use Asset Catalog Image Sets, which should contain all of the versions of an image that are necessary to support various devices and scale factors for an application. For more information, see Adding Images to an Asset Catalog Image Set.
Android - Place images in the Resources/drawable directory with Build Action: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi).
Windows Phone - Place images in the application's root directory with Build Action: Content.
Universal Windows Platform (UWP) - Place images in the application's root directory with Build Action: Content.
Then you can access them as follow:
In XAML: <Image Source="waterfront.jpg" />
In C#: var image = new Image { Source = "waterfront.jpg" };
I suggest you to read all that page of documentation to proper handling images in your Xamarin forms project.
HIH
I know it's an old question, but for people who may reach this page on future, code below works for me:
bgImage.Source="resource://FoodSnapApps.Images.steps.jpg"
Using Xamarin Forms and the NuGet FFImageloading plugin, I want to specify OS specific image paths in my XAML code like this:
<ffimageloading:CachedImage DownsampleToViewSize="False" Aspect="AspectFill">
<ffimageloading:CachedImage.Source>
<OnPlatform x:TypeArguments="ImageSource"
iOS="MyProject.iOS.Resources.header_background.png"
Android="MyProject.Droid.Resources.Drawable.header_background.png" />
</ffimageloading:CachedImage.Source>
</ffimageloading:CachedImage>
The iOS image is declared as BundleResource, while the image for Android is declared as AndroidResource. On iOS, the image is loading properly but for Android I keep getting this error:
invalid resource directory name: Error APT0000: obj/Debug/res/ myproject.droid.resources.drawable.header_background.png (APT0000) (MyProject.Android)
What is wrong? Wrong build type for image? I don't get it.
Image sources in Xamarin.Forms are based on default paths, and this has advantages, rather than direct file paths.
Place your images in
Android - /Resources/drawable
iOS - /Resources or as an asset
UWP - root project folder
Then you just reference it by name header_background.png.
This has added advantages, in that you can place #2x or greater images for iOS, and place images in drawable-hdpi or other types of drawable folders in Android, and the OS will determine the most appropriate one to get.
You have same filename header_background.png for both platforms.. So setting
Source="header_background.png" property will read automatically form droid and ios projects in appropriate resources.