Popular New Releases in Xamarin
Files
Introducing v2.1.24
maui
6.0.300-rc.1
ReactiveUI
18.0.7
uno
4.2.6
refit
v6.3.2
Popular Libraries in Xamarin
by files-community csharp
21866 MIT
A modern file manager for Windows
by dotnet csharp
14985 MIT
.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
by microsoft html
12429 MIT
This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
by mono csharp
9786 NOASSERTION
Mono open source ECMA CLI, C# and .NET implementation.
by reactiveui csharp
6886 MIT
An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
by unoplatform csharp
6427 NOASSERTION
Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
by reactiveui csharp
5880 MIT
The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.
by xamarin csharp
5556 NOASSERTION
Xamarin.Forms Official Home
by NLog csharp
5429 BSD-3-Clause
NLog - Advanced and Structured Logging for Various .NET Platforms
Trending New libraries in Xamarin
by dotnet csharp
14985 MIT
.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
by JetBrains java
2045 Apache-2.0
Skia bindings for Java
by beto-rodriguez csharp
1187 MIT
Simple, flexible, interactive & powerful charts, maps and gauges for .Net, LiveCharts2 can now practically run everywhere WPF, WinForms, Xamarin, Avalonia, WinUI, UWP, Blazor-wasm, MAUI
by Ethereal-Developers-Inc c++
864 BSD-3-Clause
OpenScan is an open-source document scanner app that enables users to scan hard copies of documents or notes and convert it into a PDF file. No ads. No data collection. We respect your privacy.
by davidortinau csharp
771 MIT
by dotnet csharp
456 MIT
by CommunityToolkit csharp
447 MIT
The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, Effects, and Behaviors to help make your life as a .NET MAUI developer easier
by HenJigg csharp
406 MIT
Example projects based on WPF, Xamarin, Blazor and other platforms
by HenJigg csharp
396 MIT
Example projects based on WPF, Xamarin, Blazor and other platforms
Top Authors in Xamarin
1
112 Libraries
770
2
74 Libraries
3750
3
63 Libraries
25375
4
57 Libraries
5586
5
48 Libraries
1467
6
43 Libraries
26468
7
41 Libraries
456
8
39 Libraries
378
9
36 Libraries
1406
10
33 Libraries
308
1
112 Libraries
770
2
74 Libraries
3750
3
63 Libraries
25375
4
57 Libraries
5586
5
48 Libraries
1467
6
43 Libraries
26468
7
41 Libraries
456
8
39 Libraries
378
9
36 Libraries
1406
10
33 Libraries
308
Trending Kits in Xamarin
No Trending Kits are available at this moment for Xamarin
Trending Discussions on Xamarin
Android emulator not responding problem on the AMD process for Xamarin
Get list of all open windows in .Net Core running on macOS (via NSApplication?)
"We've detected this app uses an unsupported version of Play billing" for Xamarin Forms Android App
MAUI: What build action for appsettings.json and how to access the file on Android?
Azure Pipelines Xamarin.Forms iOS build fails with error MSB4057: The target "_IsProjectRestoreSupported" does not exist in the project
How to check in c# code if the current project is xamarin or not?
Light and dark theme for my Maui Blazor app
Xamarin Forms HttpClient PostAsync always throw NSMallocException
Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety
exclude project references in solution on conditional c#
QUESTION
Android emulator not responding problem on the AMD process for Xamarin
Asked 2022-Mar-17 at 14:24When I update my windows to windows 11, I notice about when I run the android emulator for my Xamarin project, it freeze and said not responding.
I try these items below but won’t fix my problem :
- reduce ram size of emulator device
- reduce the resolution of emulator device
- reinstall Android SDK
- reinstall visual studio My Virtual Machine Platform and Hyper-V are enabled in my windows features.
My emulator detail is :
OS: Android 12
Ram: 4GB
Resolution : 1080x2340 pixels
Google Services: true
ANSWER
Answered 2022-Mar-17 at 14:24For the AMD process, we need to make clear to the visual studio that we use the AMD process and it should change the emulator behavior to our process.
First of all like I said we need to make sure the Virtual Machine Platform
and Hyper-V
are enabled because it’s necessary to run an android emulator in a visual studio.
Second, We need to make sure that Android Emulator Hypervisor Driver for AMD Processors
is selected in the visual studio.
Android Emulator Hypervisor Driver for the AMD Processors:
1Visual Studio > Tools > Android > Android SDK Manager > Tools > Extras > Android Emulator Hypervisor Driver for AMD Processors
2
QUESTION
Get list of all open windows in .Net Core running on macOS (via NSApplication?)
Asked 2022-Mar-07 at 07:27Getting a list of open windows in .Net Framework on Windows was relatively easy. How can I do the same in .Net Core/.Net 5 or later on macOS?
To clarify, I'm looking for a way to retrieve a list of all open windows owned by any running application/process. I don't have much experience of macOS development - I'm a Windows developer - but I've tried to use the NSApplication
as suggested by this answer.
I created a .Net 6.0 Console application in VS2022 on macOS Monterey (12.2), added a reference to Xamarin.Mac
and libxammac.dylib
as described here - which describes doing this in Xamarin rather than .Net, but I don't see any other option to create a Console application. With the simple code:
1 static void Main(string[] args)
2 {
3 NSApplication.Init();
4 }
5
I get the output
Xamarin.Mac: dlopen error: dlsym(RTLD_DEFAULT, mono_get_runtime_build_info): symbol not found
I've no idea what this means. I'm not even sure this approach has any merit.
Does anyone know if it's possible to use NSApplication from a .Net Core/6.0 application, and if so whether NSApplication will give me the ability to read a system-wide list of open windows? If not, is there another way to accomplish this?
This is only for my own internal use, it doesn't need to be in any way portable or stable outside of my own environment.
ANSWER
Answered 2022-Mar-07 at 07:27In the link you refer to, there is an important note:
... as Xamarin.Mac.dll does not run under the .NET Core runtime, it only runs with the Mono runtime.
Because you try to run Xamarin.Mac.dll
under .net-core
, you get this dlopen
error.
No System-wide List via NSApplication
The linked answer with NSApplication.shared.windows is incorrect if you want to read a system-wide list of open windows. It can only be used to determine all currently existing windows for the application from which the call is made, see Apple's documentation.
Alternative solution
Nevertheless, there are several ways to access the Window information in macOS. One of them could be a small unmanaged C-lib that gets the necessary information via CoreFoundation and CoreGraphics and returns it to C# via Platform Invoke (P/Invoke).
Native Code
Here is example code for a C-Lib that determines and returns the names of the window owners.
WindowsListLib.h
1 static void Main(string[] args)
2 {
3 NSApplication.Init();
4 }
5extern char const **windowList(void);
6extern void freeWindowList(char const **list);
7
The interface of the library consists of only two functions. The first function called windowList
returns a list with the names of the window owners. The last element of the list must be NULL so that you can detect where the list ends on the managed C# side. Since the memory for the string list is allocated dynamically, you must use the freeWindowList
function to free the associated memory after processing.
WindowsListLib.c
1 static void Main(string[] args)
2 {
3 NSApplication.Init();
4 }
5extern char const **windowList(void);
6extern void freeWindowList(char const **list);
7#include "WindowListLib.h"
8#include <CoreFoundation/CoreFoundation.h>
9#include <CoreGraphics/CoreGraphics.h>
10
11static void errorExit(char *msg) {
12 fprintf(stderr, "%s\n", msg);
13 exit(1);
14}
15
16static char *copyUTF8String(CFStringRef string) {
17 CFIndex length = CFStringGetLength(string);
18 CFIndex size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
19 char *buf = malloc(size);
20 if(!buf) {
21 errorExit("malloc failed");
22 }
23 if(!CFStringGetCString(string, buf, size, kCFStringEncodingUTF8)) {
24 errorExit("copyUTF8String with utf8 encoding failed");
25 }
26 return buf;
27}
28
29
30char const **windowList(void) {
31 CFArrayRef cfWindowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
32 CFIndex count = CFArrayGetCount(cfWindowList);
33 char const **list = malloc(sizeof(char *) * (count + 1));
34 if(!list) {
35 errorExit("malloc failed");
36 }
37 list[count] = NULL;
38 for(CFIndex i = 0; i < count; i++) {
39 CFDictionaryRef windowInfo = CFArrayGetValueAtIndex(cfWindowList, i);
40 CFStringRef name = CFDictionaryGetValue(windowInfo, kCGWindowOwnerName);
41 if(name) {
42 list[i] = copyUTF8String(name);
43 } else {
44 list[i] = strdup("unknown");
45 }
46 }
47 CFRelease(cfWindowList);
48 return list;
49}
50
51void freeWindowList(char const **list) {
52 const char **ptr = list;
53 while(*ptr++) {
54 free((void *)*ptr);
55 }
56 free(list);
57}
58
CGWindowListCopyWindowInfo
is the actual function that gets the window information. It returns a list of dictionaries containing the details. From this we extract kCGWindowOwnerName
. This CFStringRef
is converted to a dynamically allocated UTF-8 string by the function copyUTF8String
.
By convention, calls like CGWindowListCopyWindowInfo
that contain the word copy
(or create
) must be released after use with CFRelease
to avoid creating memory leaks.
C# Code
The whole thing can then be called on the C# side something like this:
1 static void Main(string[] args)
2 {
3 NSApplication.Init();
4 }
5extern char const **windowList(void);
6extern void freeWindowList(char const **list);
7#include "WindowListLib.h"
8#include <CoreFoundation/CoreFoundation.h>
9#include <CoreGraphics/CoreGraphics.h>
10
11static void errorExit(char *msg) {
12 fprintf(stderr, "%s\n", msg);
13 exit(1);
14}
15
16static char *copyUTF8String(CFStringRef string) {
17 CFIndex length = CFStringGetLength(string);
18 CFIndex size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
19 char *buf = malloc(size);
20 if(!buf) {
21 errorExit("malloc failed");
22 }
23 if(!CFStringGetCString(string, buf, size, kCFStringEncodingUTF8)) {
24 errorExit("copyUTF8String with utf8 encoding failed");
25 }
26 return buf;
27}
28
29
30char const **windowList(void) {
31 CFArrayRef cfWindowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
32 CFIndex count = CFArrayGetCount(cfWindowList);
33 char const **list = malloc(sizeof(char *) * (count + 1));
34 if(!list) {
35 errorExit("malloc failed");
36 }
37 list[count] = NULL;
38 for(CFIndex i = 0; i < count; i++) {
39 CFDictionaryRef windowInfo = CFArrayGetValueAtIndex(cfWindowList, i);
40 CFStringRef name = CFDictionaryGetValue(windowInfo, kCGWindowOwnerName);
41 if(name) {
42 list[i] = copyUTF8String(name);
43 } else {
44 list[i] = strdup("unknown");
45 }
46 }
47 CFRelease(cfWindowList);
48 return list;
49}
50
51void freeWindowList(char const **list) {
52 const char **ptr = list;
53 while(*ptr++) {
54 free((void *)*ptr);
55 }
56 free(list);
57}
58using System.Runtime.InteropServices;
59
60namespace WindowList
61{
62 public static class Program
63 {
64 [DllImport("WindowListLib", EntryPoint = "windowList")]
65 private static extern IntPtr WindowList();
66
67 [DllImport("WindowListLib", EntryPoint = "freeWindowList")]
68 private static extern void FreeWindowList(IntPtr list);
69
70 private static List<string> GetWindows()
71 {
72 var nativeWindowList = WindowList();
73 var windows = new List<string>();
74 var nativeWindowPtr = nativeWindowList;
75 string? windowName;
76 do
77 {
78 var strPtr = Marshal.ReadIntPtr(nativeWindowPtr);
79 windowName = Marshal.PtrToStringUTF8(strPtr);
80 if (windowName == null) continue;
81 windows.Add(windowName);
82 nativeWindowPtr += Marshal.SizeOf(typeof(IntPtr));
83 } while (windowName != null);
84
85 FreeWindowList(nativeWindowList);
86 return windows;
87 }
88
89
90 static void Main()
91 {
92 foreach (var winName in GetWindows())
93 {
94 Console.WriteLine(winName);
95 }
96 }
97 }
98}
99
The GetWindows
method fetches the data via a native call to WindowList
and converts the C strings to managed strings, then releases the native resources via a call to FreeWindowList
.
This function returns only the owner names, such as Finder, Xcode, Safari, etc. If there are multiple windows, the owners will also be returned multiple times, etc. The exact logic of what should be determined will probably have to be changed according to your requirements. However, the code above should at least show a possible approach to how this can be done.
Screenshot
QUESTION
"We've detected this app uses an unsupported version of Play billing" for Xamarin Forms Android App
Asked 2022-Feb-22 at 05:46When attempting to publish my Xamarin Forms app, I get the following error from Google Play console
We've detected this app uses an unsupported version of Play billing. Please upgrade to Billing Library version 3 or newer to publish this app.
I don't use any billing library. I've tried updating all nuget dependencies, to no luck. How do I fix this error so I can publish my app?
ANSWER
Answered 2021-Nov-11 at 23:11Apparently I had BILLING
permissions set in my manifest. Removing these fixed the issue.
You can do this by right-clicking the project --> Properties --> Android Manifest --> Searching for BILLING
under "Required Permissions" and unchecking it
Alternatively, open AndroidManifest.xml
and delete the line
1<uses-permission android:name="com.android.vending.BILLING" />
2
QUESTION
MAUI: What build action for appsettings.json and how to access the file on Android?
Asked 2022-Feb-20 at 02:47I created an appsettings file for a MAUI app and loading it in the IConfiguration using .Host.ConfigureAppConfiguration
on the builder from a MauiApp.CreateBuilder(); I can access the file in Windows but not when running the Android emulator. The code:
1 var builder = MauiApp.CreateBuilder();
2 builder
3 .RegisterBlazorMauiWebView()
4 .UseMauiApp<App>()
5 .ConfigureFonts(fonts =>
6 {
7 fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
8 })
9 .Host
10 .ConfigureAppConfiguration((app, config) =>
11 {
12#if __ANDROID__
13 // https://stackoverflow.com/questions/49867588/accessing-files-through-a-physical-path-in-xamarin-android
14 //var documentsFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
15 config.AddJsonFile( "appsettings.json", optional: false, reloadOnChange: true);
16#endif
17
18#if WINDOWS10_0_17763_0_OR_GREATER
19 //https://stackoverflow.com/questions/69000474/how-to-load-app-configuration-from-appsettings-json-in-maui-startup
20 Assembly callingAssembly = Assembly.GetEntryAssembly();
21 Version versionRuntime = callingAssembly.GetName().Version;
22 string assemblyLocation = Path.GetDirectoryName(System.AppContext.BaseDirectory); //CallingAssembly.Location
23 var configFile = Path.Combine(assemblyLocation, "appsettings.json");
24 config.AddJsonFile(configFile, optional: false, reloadOnChange: true);
25#endif
26 });
27
Mockup project is here
ANSWER
Answered 2021-Dec-09 at 02:36There is an open issue Add support for appsetting.json that shows this code snippet as current work-around:
1 var builder = MauiApp.CreateBuilder();
2 builder
3 .RegisterBlazorMauiWebView()
4 .UseMauiApp<App>()
5 .ConfigureFonts(fonts =>
6 {
7 fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
8 })
9 .Host
10 .ConfigureAppConfiguration((app, config) =>
11 {
12#if __ANDROID__
13 // https://stackoverflow.com/questions/49867588/accessing-files-through-a-physical-path-in-xamarin-android
14 //var documentsFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
15 config.AddJsonFile( "appsettings.json", optional: false, reloadOnChange: true);
16#endif
17
18#if WINDOWS10_0_17763_0_OR_GREATER
19 //https://stackoverflow.com/questions/69000474/how-to-load-app-configuration-from-appsettings-json-in-maui-startup
20 Assembly callingAssembly = Assembly.GetEntryAssembly();
21 Version versionRuntime = callingAssembly.GetName().Version;
22 string assemblyLocation = Path.GetDirectoryName(System.AppContext.BaseDirectory); //CallingAssembly.Location
23 var configFile = Path.Combine(assemblyLocation, "appsettings.json");
24 config.AddJsonFile(configFile, optional: false, reloadOnChange: true);
25#endif
26 });
27var builder = MauiApp.CreateBuilder();
28 builder
29 .RegisterBlazorMauiWebView()
30 .UseMauiApp<App>()
31 .ConfigureFonts(fonts =>
32 {
33 fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
34 })
35 .Host
36 .ConfigureAppConfiguration((app, config) =>
37 {
38 var assembly = typeof(App).GetTypeInfo().Assembly;
39 config.AddJsonFile(new EmbeddedFileProvider(assembly), "appsettings.json", optional: false, false);
40 });
41
Note the use of EmbeddedFileProvider
. This works with Build Action EmbeddedResource
.
QUESTION
Azure Pipelines Xamarin.Forms iOS build fails with error MSB4057: The target "_IsProjectRestoreSupported" does not exist in the project
Asked 2022-Jan-26 at 09:39We have Xamarin.Forms solution with iOS and UWP projects. We use Azure pipelines to build the iOS project. Until yesterday everything was working fine. Now the build fails at the NuGet Restore step with the error:
##[error]The nuget command failed with exit code(1) and error(/Users/runner/work/1/s/"MyProjectName.UWP".csproj : error MSB4057: The target "_IsProjectRestoreSupported" does not exist in the project.
We can see that the problem occurs when trying to restore NuGet packages for the UWP project on the Mac OS build host. Image: macOS-11
Workaround will be to exclude it from the solution, but we are using it for testing purposes and this is not a good option for us.
ANSWER
Answered 2022-Jan-26 at 09:39- Updated Answer This can be resolved using MSBuild task instead, as the collogues mentioned below. However, in my case this still lead to the same error. After investigating the pipeline. The root cause has been identified: Both in MSBuild@1 and XamariniOS@2 tasks you should target the specific iOS Project and not the solution file, like so:
" - task: MSBuild@1 inputs: solution: 'PathToIosProject/myproject.iOS.csproj' configuration: '$(BuildConfiguration)' msbuildArguments: /t:restore
" - task: XamariniOS@2 inputs: solutionFile: PathToIosProject/myproject.iOS.csproj' configuration: '$(BuildConfiguration)' packageApp: true signingIdentity: $(APPLE_CERTIFICATE_SIGNING_IDENTITY)
- Old Answer
We have managed to resolve the issue. The root cause of it seems to be an update to the mono framework in the MacOS pipeline build agent. In order to fix it, we need a script for downloading and installing an older version of the mono framework as a first step of the build pipeline like so: link to the pipeline tasks photo
This is the code of the bash scrip used in the task:
#!/bin/bash set -ex
MONO_MACOS_PKG_DOWNLOAD_URL='https://download.mono-project.com/archive/6.12.0/macos-10-universal/MonoFramework-MDK-6.12.0.100.macos10.xamarin.universal.pkg'
mkdir -p /tmp/mono-install cd /tmp/mono-install
mono --version
wget -q -O ./mono-installer.pkg "$MONO_MACOS_PKG_DOWNLOAD_URL"
sudo installer -pkg ./mono-installer.pkg -target /
mono --version
ls -alh /Library/Frameworks/Mono.framework/Versions/Current
QUESTION
How to check in c# code if the current project is xamarin or not?
Asked 2021-Dec-19 at 12:15I am looking for a preprocessor symbol that would allow me to compile different code based on whether a project is xamarin or not.
1void a()
2{
3#if XAMARIN
4 b();
5#else
6 c();
7#endif
8}
9
ANSWER
Answered 2021-Dec-19 at 12:15I would suggest you don't use preprocessor symbols, but abstract stuff in separate classes for each platform. Then inject the specific implementation at runtime.
If you really must use these, you can always define your own symbols as needed for each configuration or target framework.
Just create a file called Directory.Build.targets
(casing matters!) in the root of your repo. Usually next to your .sln
file.
In this Directory.Build.targets
you can define symbols like so:
1void a()
2{
3#if XAMARIN
4 b();
5#else
6 c();
7#endif
8}
9<Project>
10 <PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
11 <DefineConstants>$(DefineConstants);NETSTANDARD;PORTABLE</DefineConstants>
12 </PropertyGroup>
13 <PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
14 <DefineConstants>$(DefineConstants);NET;WPF;XAML</DefineConstants>
15 </PropertyGroup>
16 <PropertyGroup Condition="$(TargetFramework.StartsWith('uap'))">
17 <DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS;WINDOWS_UWP;UWP</DefineConstants>
18 </PropertyGroup>
19 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
20 <DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA;APPLE;IOS</DefineConstants>
21 </PropertyGroup>
22 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.mac'))">
23 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;MAC</DefineConstants>
24 </PropertyGroup>
25 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.tvos'))">
26 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;TVOS</DefineConstants>
27 </PropertyGroup>
28 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.watchos'))">
29 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;WATCHOS</DefineConstants>
30 </PropertyGroup>
31 <PropertyGroup Condition="$(TargetFramework.StartsWith('monoandroid'))">
32 <DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(TargetFramework)' == 'monoandroid10.0'">
35 <DefineConstants>$(DefineConstants);MONO;ANDROID;__ANDROID_29__;</DefineConstants>
36 </PropertyGroup>
37 <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
38 <DefineConstants>$(DefineConstants);NETCORE;NETCOREAPP</DefineConstants>
39 </PropertyGroup>
40 <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp3.'))">
41 <DefineConstants>$(DefineConstants);WPF</DefineConstants>
42 </PropertyGroup>
43 <PropertyGroup Condition="$(TargetFramework.StartsWith('tizen'))">
44 <DefineConstants>$(DefineConstants);TIZEN</DefineConstants>
45 </PropertyGroup>
46</Project>
47
Then you can use these in your project as needed:
1void a()
2{
3#if XAMARIN
4 b();
5#else
6 c();
7#endif
8}
9<Project>
10 <PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
11 <DefineConstants>$(DefineConstants);NETSTANDARD;PORTABLE</DefineConstants>
12 </PropertyGroup>
13 <PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
14 <DefineConstants>$(DefineConstants);NET;WPF;XAML</DefineConstants>
15 </PropertyGroup>
16 <PropertyGroup Condition="$(TargetFramework.StartsWith('uap'))">
17 <DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS;WINDOWS_UWP;UWP</DefineConstants>
18 </PropertyGroup>
19 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
20 <DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA;APPLE;IOS</DefineConstants>
21 </PropertyGroup>
22 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.mac'))">
23 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;MAC</DefineConstants>
24 </PropertyGroup>
25 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.tvos'))">
26 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;TVOS</DefineConstants>
27 </PropertyGroup>
28 <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.watchos'))">
29 <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;WATCHOS</DefineConstants>
30 </PropertyGroup>
31 <PropertyGroup Condition="$(TargetFramework.StartsWith('monoandroid'))">
32 <DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(TargetFramework)' == 'monoandroid10.0'">
35 <DefineConstants>$(DefineConstants);MONO;ANDROID;__ANDROID_29__;</DefineConstants>
36 </PropertyGroup>
37 <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
38 <DefineConstants>$(DefineConstants);NETCORE;NETCOREAPP</DefineConstants>
39 </PropertyGroup>
40 <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp3.'))">
41 <DefineConstants>$(DefineConstants);WPF</DefineConstants>
42 </PropertyGroup>
43 <PropertyGroup Condition="$(TargetFramework.StartsWith('tizen'))">
44 <DefineConstants>$(DefineConstants);TIZEN</DefineConstants>
45 </PropertyGroup>
46</Project>
47#if PORTABLE
48 // do portable stuff
49#elif UWP
50 // do uwp stuff
51#endif
52
QUESTION
Light and dark theme for my Maui Blazor app
Asked 2021-Dec-10 at 03:23I would like to implement light and dark theme inside of my Maui Blazor application. As you know, Blazor is nothing other than Html and Css so I easily implement dark and light theme for the content of my app (thanks to a simple .dark class added on the html tag).
The problem I am facing is for the upper and lower part of the Maui application.
Let me show you by an example.
Android version
iOS version
The pictures above show you the current situation: on the left, the light theme and on the right, the dark theme. As you can see, the dark theme is problematic for the top and bottom sections (only the top section is problematic for the iOS version).
What I would like to achieve: the top and bottom sections should be colored the same as the content of the page:
- #292929 for the Dark theme
- #FFFFFF for the Light theme
Something like the picture below: when switching to the dark theme, everything is coloured.
I have no idea how to achieve this for Maui Blazor and I have no experience with Xamarin.
Can you point me to the right direction please ?
As you probably know, BlazorWebView enables you to host a Blazor web application right in the .NET MAUI application.
ANSWER
Answered 2021-Dec-10 at 03:23Within the next month or two, it should become possible to do this via StatusBarEffect
and NavigationBarEffect
in MAUI Community Toolkit. Discussion here - for Xamarin Toolkit.
Those effects are currently in branch xamarin.develop
of Xamarin.Community.Toolkit
. The change will also get merged to Maui Toolkit, not sure when exactly.
Below is based on that source code, adapted to work with MAUI. Tested.
Android:
1public class MainActivity : MauiAppCompatActivity
2{
3 protected override void OnCreate(Android.OS.Bundle savedInstanceState)
4 {
5 base.OnCreate(savedInstanceState);
6
7 SetColors();
8 }
9
10 private void SetColors()
11 {
12 if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) {
13 // Aqua.
14 var color = Microsoft.Maui.Graphics.Color.FromRgb(0, 255, 255);
15 // The thin bar at top of Android screen.
16 Window.SetStatusBarColor(color.ToNative());
17 // The thicker bar at bottom of Android screen.
18 Window.SetNavigationBarColor(color.ToNative());
19
20 }
21 }
22
23}
24
That sets the colors when the app starts up. The effects will contain the extra logic needed to set those colors dynamically. (Until those effects exist, you could save a value in persistent storage, force your app to restart, read that flag back to know what color to apply. OR you can make a dependency service in Android, to call from X-Forms code. Will need to use Device.BeginInvokeOnMainThread
.)
iOS:
...TBD...
Setting status bar color can be seen in ShaXam/iOS/StatusBarStyleManager/.
The essential lines are these (I think):
1public class MainActivity : MauiAppCompatActivity
2{
3 protected override void OnCreate(Android.OS.Bundle savedInstanceState)
4 {
5 base.OnCreate(savedInstanceState);
6
7 SetColors();
8 }
9
10 private void SetColors()
11 {
12 if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) {
13 // Aqua.
14 var color = Microsoft.Maui.Graphics.Color.FromRgb(0, 255, 255);
15 // The thin bar at top of Android screen.
16 Window.SetStatusBarColor(color.ToNative());
17 // The thicker bar at bottom of Android screen.
18 Window.SetNavigationBarColor(color.ToNative());
19
20 }
21 }
22
23}
24UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
25
26GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
27
iOS - Not tested.
QUESTION
Xamarin Forms HttpClient PostAsync always throw NSMallocException
Asked 2021-Dec-01 at 09:02We are building a mobile app for iOS and Android using Xamarin Forms 5 and using Visual Studio 2022. When we make a Post request to any api, both our own as external api's we are always returned:
Xamarin.PreBuilt.iOS[3728:2199180] Xamarin.iOS: Received unhandled ObjectiveC exception: NSMallocException Failed to grow buffer
GET request work fine. I have searched Google and StackOverflow but can not find any help. I have tried to increase the HttpClient.MaxResponseContentBufferSize without any difference.
The app for now is very simple, one page with a button to test. Code behind is as followed:
1public partial class MainPage : ContentPage
2 {
3 public MainPage()
4 {
5 InitializeComponent();
6 }
7
8 private async void Button_Clicked(object sender, EventArgs e)
9 {
10 var client = new HttpClient();
11 client.BaseAddress = new Uri("https://ptsv2.com/");
12
13 string jsonData = @"{}";
14
15 var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
16 HttpResponseMessage response = await client.PostAsync("/t/l4x2e-1637576441/post", content);
17
18 var result = await response.Content.ReadAsStringAsync();
19 }
20 }
21
This fails on iOS linked device, but for Android Simulator it works. Is there anything I need to change for iOS?
Update
The code works on multiple machines except mine, trying to figure out what setting this could be, any ideas would be greatly appreciated.
ANSWER
Answered 2021-Dec-01 at 09:02I've had exactly the same problem, and have logged a ticket with Microsoft on the VS feedback forums. And then today I found a simple work-around. At least I assume it's a work-around and not a solution. Where I had
1public partial class MainPage : ContentPage
2 {
3 public MainPage()
4 {
5 InitializeComponent();
6 }
7
8 private async void Button_Clicked(object sender, EventArgs e)
9 {
10 var client = new HttpClient();
11 client.BaseAddress = new Uri("https://ptsv2.com/");
12
13 string jsonData = @"{}";
14
15 var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
16 HttpResponseMessage response = await client.PostAsync("/t/l4x2e-1637576441/post", content);
17
18 var result = await response.Content.ReadAsStringAsync();
19 }
20 }
21HttpClient client = new HttpClient()
22
I instead do this:
1public partial class MainPage : ContentPage
2 {
3 public MainPage()
4 {
5 InitializeComponent();
6 }
7
8 private async void Button_Clicked(object sender, EventArgs e)
9 {
10 var client = new HttpClient();
11 client.BaseAddress = new Uri("https://ptsv2.com/");
12
13 string jsonData = @"{}";
14
15 var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
16 HttpResponseMessage response = await client.PostAsync("/t/l4x2e-1637576441/post", content);
17
18 var result = await response.Content.ReadAsStringAsync();
19 }
20 }
21HttpClient client = new HttpClient()
22var handler = new HttpClientHandler();
23HttpClient client = new HttpClient(handler);
24
And bingo, it now works.
QUESTION
Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety
Asked 2021-Nov-17 at 20:38After upgrade my Xamarin.Forms version to 5.0.0.2244, I'm getting the following warning in Main.cs
file inside my iOS project:
1Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety.
2
This my Main.cs
file:
1Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety.
2using UIKit;
3
4namespace LindeGctMobileApplication.iOS
5{
6 public class Application
7 {
8 private static void Main(string[] args)
9 {
10 UIApplication.Main(args, null, "AppDelegate"); // << warning
11 }
12 }
13}
14
What I need to change to remove this warning?
ANSWER
Answered 2021-Nov-17 at 20:38Class reference through a string is now deprecated. You need to change this line:
1Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety.
2using UIKit;
3
4namespace LindeGctMobileApplication.iOS
5{
6 public class Application
7 {
8 private static void Main(string[] args)
9 {
10 UIApplication.Main(args, null, "AppDelegate"); // << warning
11 }
12 }
13}
14UIApplication.Main(args, null, "AppDelegate");
15
to this:
1Method 'UIKit.UIApplication.Main' is obsolete: Use the overload with 'Type' instead of 'String' parameters for type safety.
2using UIKit;
3
4namespace LindeGctMobileApplication.iOS
5{
6 public class Application
7 {
8 private static void Main(string[] args)
9 {
10 UIApplication.Main(args, null, "AppDelegate"); // << warning
11 }
12 }
13}
14UIApplication.Main(args, null, "AppDelegate");
15UIApplication.Main(args, null, typeof(AppDelegate));
16
In that way, you explicitly inform the type of the class.
QUESTION
exclude project references in solution on conditional c#
Asked 2021-Nov-05 at 07:52I build xamarin app(uwp,ios,android) and use appCenter for builds and delivery. For certain reason i cant build android and ios project having uwp project included in the solution. Creating build configuration does solve an issue. Is its possible to have some conditional in solution file to include certain projects?
ANSWER
Answered 2021-Nov-05 at 07:52There is a Nuget restore issue with the latest Mono version. You can try lowering the Mono version, used to build the application in Appcenter. Go to AppCenter -> select Build from the left menu. Then choose a branch. At the most right you will see a setting icon. Click on it. From there menu like this will appear. From the SDK version you can choose a different one.
Community Discussions contain sources that include Stack Exchange Network
Tutorials and Learning Resources in Xamarin
Tutorials and Learning Resources are not available at this moment for Xamarin