C
C#3y ago
Mek

✅ asking user where to save file avalonia

// MainWindowViewModel.cs
public void ShareItem()
{
var vm = new ShareItemViewModel(List.SelectedItem);
}

// ShareItemViewModel.cs
public class ShareItemViewModel : ViewModelBase
{
public ShareItemViewModel(DiaryModel selectedItem)
{
}
}
// MainWindowViewModel.cs
public void ShareItem()
{
var vm = new ShareItemViewModel(List.SelectedItem);
}

// ShareItemViewModel.cs
public class ShareItemViewModel : ViewModelBase
{
public ShareItemViewModel(DiaryModel selectedItem)
{
}
}
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
Date: <- date of entry
Time: <- time of entry
Title: <- title of entry
Entry: <- the entry itself
Date: <- date of entry
Time: <- time of entry
Title: <- title of entry
Entry: <- the entry itself
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
Mek
MekOP3y ago
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.
Angius
Angius3y ago
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
Mek
MekOP3y ago
ok I'll give it a shot ok so how can I put this in an asyncronous function in that ShareItemViewModel?
Mek
MekOP3y ago
ero
ero3y ago
Using a Microsoft file type in Avalonia seems like a bad idea What's an iOS user gonna do with it
Mek
MekOP3y ago
This isn't being built for mobile use of any sort. Desktop only
ero
ero3y ago
What's a Mac user gonna do with it
Mek
MekOP3y ago
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.
Angius
Angius3y ago
Also, just FYI, Office files are a PITA to deal with Why not something simpler, like JSON, Markdown with front-matter or something?
Denis
Denis3y ago
LaTeX when
Angius
Angius3y ago
C# has a built-in JSON serializer
Mek
MekOP3y ago
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
ero
ero3y ago
Well are they supposed to use them or read them?
Denis
Denis3y ago
Aren't you trying to just save your Todo app data that only serves your Todo app?
Mek
MekOP3y ago
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
ero
ero3y ago
Who would then presumably load it in their instance of the app?
Mek
MekOP3y ago
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 file
Angius
Angius3y ago
You'd probably benefit from using an actual database in this case, like SQLite or LiteDB
Mek
MekOP3y ago
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
Angius
Angius3y ago
Sure thing, JSON files for now it is
Mek
MekOP3y ago
Thank you. so now back to my question on how to get this code to work please.
Angius
Angius3y ago
The errors are helpful there You can only await inside of async methods A constructor cannot be async
Mek
MekOP3y ago
ok so I've done this
Mek
MekOP3y ago
but it doesn't like this inside the parenthesis so what can I do instead?
Angius
Angius3y ago
What's the error?
Mek
MekOP3y ago
Argument 1: cannot convert from 'Diary.ViewModels.ShareItemViewModel' to 'Avalonia.Visual'
Angius
Angius3y ago
Ah, huh, probably means the code isn't supposd to be called from a viewmodel...?
Mek
MekOP3y ago
usercontrol my fault
Angius
Angius3y ago
In the example, it was called from a view That inherits from UserControl
Mek
MekOP3y ago
MainWindowView is a window and MainWindowViewModel is a view model base. These inherit their information to display from DiaryListView (UserControl) and DiaryListViewModel (ViewModelBase)
Kouhai
Kouhai3y ago
It would be much better to use dependecy injection for this
Mek
MekOP3y ago
I don't know what that means and the docs only show that so there's no guide from what I'm seeing
Kouhai
Kouhai3y ago
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
Mek
MekOP3y ago
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?
Kouhai
Kouhai3y ago
I'm not really sure how new avalonia is, but I'm pretty sure it's older than MAUI
Kouhai
Kouhai3y ago
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#.
Angius
Angius3y ago
You can always ask in more specialized channels, #gui in this case
Mek
MekOP3y ago
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

Did you find this page helpful?