Hello fellow programmers,
I'm pretty much a novice trying to teach himself C# by creating a MAUI app. I'm in the very early stages of planning since I am genuinely playing around with the software to see what can be done. I would love for my app to do a million things but the lack of experience gets in the way so one has to start somewhere.
So here's the first part of my question:
Say I have this button:
<div class="input-group">
<button for="directoryInput" class="btn btn-primary" @onclick="newDir">Create Folder</button>
<input type="text" @bind-value="tempDirectory" id="directoryInput" placeholder="..\ enter directory name" />
And this function:
private void newDir(){
String t = new(workingDirectory + "\\" + tempDirectory);
try {
// Determine whether the directory exists.
if (Directory.Exists(t)) { rollingLabel = tempDirectory + " already exists!"; return; }
// Try to create the directory.
Directory.CreateDirectory(t);
rollingLabel = tempDirectory + " was created successfully";
} catch (Exception e){ rollingLabel = e.Message.ToString(); }
finally { directoryList = GetDirectoryList(); }
What function, class, or variable can I use to change the logic if it's in ran in Android or iOS. For example:
if(platform==windows) newdir();
if(platform==android) rollingLabel="This function is not available in Android";
if(platform==ios) rollingLabel="this function is not available in iOS";
The other part to my question is about exception handling.
When I wrote the code for Windows it worked perfectly. High on success, I ran the code on the Android emulator and it broke the app without hopes to recover. I understand that the file system is not the same and that is where I messed up (I think), but I should had gotten an error saying sorry it can't be done or something, right?
So how do you handle such case where the error could be different in Android vs Window or iOS?
Thank you!
BMET1
I don't know how to begin to fix this issue, or maybe I'm going about it the wrong way.
For the Maui Blazor Hybrid, Platform code can be invoked from cross-platform code by using conditional compilation. You can use it like the following code.
#if WINDOWS
private void newDir(){...}
#endif
#if ANDROID
private void newDir(){rollingLabel="This function is not available in Android";}
#endif
#if IOS
private void newDir(){rollingLabel="This function is not available in iOS";}
#endif
For more details about conditional compilation, you can refer to Invoke platform code. Although the document is about .NET MAUI, it also applies to MAUI Blazor Hybrid.