✅ asking user where to save file avalonia
I'm wanting to use the ShareItemViewModel to prompt the user via their file explorer on where to save their entry that they want to share. When they select a location, I would like for the code to generate a Microsoft Word Document with the information saved within in the manor of
and then display a success label across the bottom of the home screen that their action has been successful. The application I'm working with is located here https://github.com/mekasu0124/Diary. Thanks in advance.
38 Replies
I did attempt this on my own first, and found this https://docs.avaloniaui.net/docs/next/basics/user-interface/file-dialogs but I do not know how to implement this into my existing code.
It's fairly self explanatory
The code in the docs should work just out of the box: https://docs.avaloniaui.net/docs/next/basics/user-interface/file-dialogs#savefilepickerasync
ok I'll give it a shot
ok so how can I put this in an asyncronous function in that ShareItemViewModel?
Using a Microsoft file type in Avalonia seems like a bad idea
What's an iOS user gonna do with it
This isn't being built for mobile use of any sort. Desktop only
What's a Mac user gonna do with it
https://www.apple.com/macos/compatibility/#:~:text=So%20you%20can%20use%20Word,all%20at%20the%20same%20time.
according to apple, they can use them just fine. They'll have to have microsoft office just like window users.
Also, just FYI, Office files are a PITA to deal with
Why not something simpler, like JSON, Markdown with front-matter or something?
LaTeX
C# has a built-in JSON serializer
I mean I can be really basic about it and just create a .txt file, but I was just trying to be different. I can use a text file. it's no big deal
I don't want to export a json file as majority of non-programming/non-gaming users don't know what to use them for or how to read them
so I'm fine with a text file
Well are they supposed to use them or read them?
Aren't you trying to just save your Todo app data that only serves your Todo app?
yes. the purpose of exporting the entry to a text file of some sort is primarily for the use of those who go to counceling. They can export the entry to a text file, attach it to an email, and then send it to whomever they like, including but not limited to a councelor
Who would then presumably load it in their instance of the app?
no. I've been had that figured out. It saves to a database.json file. With the Todo app, I was trying to incorporate the update feature but it's more a headache than its worth
they wouldn't load it into an app? It exports the entry to a
entry_title.txt
file and saves to the users designated location. The file is now independent from the app. It's just a regular text fileYou'd probably benefit from using an actual database in this case, like SQLite or LiteDB
I just want to export the entry to a text file. I just learned how to work with json files a day or two ago. I have no idea how to work with a database and I don't want to restructure half my code to do so at this time. I can pin that for a future update
Sure thing, JSON files for now it is
Thank you. so now back to my question on how to get this code to work please.
The errors are helpful there
You can only
await
inside of async
methods
A constructor cannot be async
ok so I've done this
but it doesn't like
this
inside the parenthesis so what can I do instead?What's the error?
Argument 1: cannot convert from 'Diary.ViewModels.ShareItemViewModel' to 'Avalonia.Visual'
Ah, huh, probably means the code isn't supposd to be called from a viewmodel...?
usercontrol my fault
In the example, it was called from a view
That inherits from
UserControl
MainWindowView is a window and MainWindowViewModel is a view model base. These inherit their information to display from DiaryListView (UserControl) and DiaryListViewModel (ViewModelBase)
It would be much better to use dependecy injection for this
I don't know what that means and the docs only show that so there's no guide from what I'm seeing
Oh nvm I thought the project already was using dependency injection
If you don't want to use dependency injection you could just have a public static window instance, but the correct way imo would be dependency injection
I wouldn't mind to learn it, but avalonia doesn't have a guide on it from what I can find
just out of curiousity. How new is avalonia? Because if it's new new then would MAUI or WPF be better since they're old enough to have a lot of guides and such?
I'm not really sure how new avalonia is, but I'm pretty sure it's older than MAUI
Dependency injection is just a technique/pattern that you can use with pretty much any framework
https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
Dependency injection - .NET
Learn how to use dependency injection within your .NET apps. Discover how to registration services, define service lifetimes, and express dependencies in C#.
You can always ask in more specialized channels, #gui in this case
this is way over my head. I appreciate it, but it's just too up there for me to teach to myself.
I appreciate the referral, but just simply wanting to create a text file that only writes 4 things and save it to a target location is becoming entirely more complicated than I thought it would be, so I'm just going to remove the share button and it's code for now and just pin it for a later update.
I appreciate the help guys. Have fun