C
C#11mo ago
Mekasu0124

✅ 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
Mekasu0124
Mekasu012411mo 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
Angius11mo 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
Mekasu0124
Mekasu012411mo ago
ok I'll give it a shot ok so how can I put this in an asyncronous function in that ShareItemViewModel?
Mekasu0124
Mekasu012411mo ago
ero
ero11mo ago
Using a Microsoft file type in Avalonia seems like a bad idea What's an iOS user gonna do with it
Mekasu0124
Mekasu012411mo ago
This isn't being built for mobile use of any sort. Desktop only
ero
ero11mo ago
What's a Mac user gonna do with it
Mekasu0124
Mekasu012411mo 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
Angius11mo 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
Denis11mo ago
LaTeX when
Angius
Angius11mo ago
C# has a built-in JSON serializer
Mekasu0124
Mekasu012411mo 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
ero11mo ago
Well are they supposed to use them or read them?
Denis
Denis11mo ago
Aren't you trying to just save your Todo app data that only serves your Todo app?
Mekasu0124
Mekasu012411mo 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
ero11mo ago
Who would then presumably load it in their instance of the app?
Mekasu0124
Mekasu012411mo 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
Angius11mo ago
You'd probably benefit from using an actual database in this case, like SQLite or LiteDB
Mekasu0124
Mekasu012411mo 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
Angius11mo ago
Sure thing, JSON files for now it is
Mekasu0124
Mekasu012411mo ago
Thank you. so now back to my question on how to get this code to work please.
Angius
Angius11mo ago
The errors are helpful there You can only await inside of async methods A constructor cannot be async
Mekasu0124
Mekasu012411mo ago
ok so I've done this
Mekasu0124
Mekasu012411mo ago
but it doesn't like this inside the parenthesis so what can I do instead?
Angius
Angius11mo ago
What's the error?
Mekasu0124
Mekasu012411mo ago
Argument 1: cannot convert from 'Diary.ViewModels.ShareItemViewModel' to 'Avalonia.Visual'
Angius
Angius11mo ago
Ah, huh, probably means the code isn't supposd to be called from a viewmodel...?
Mekasu0124
Mekasu012411mo ago
usercontrol my fault
Angius
Angius11mo ago
In the example, it was called from a view That inherits from UserControl
Mekasu0124
Mekasu012411mo 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
Kouhai11mo ago
It would be much better to use dependecy injection for this
Mekasu0124
Mekasu012411mo 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
Kouhai11mo 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
Mekasu0124
Mekasu012411mo 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
Kouhai11mo ago
I'm not really sure how new avalonia is, but I'm pretty sure it's older than MAUI
Kouhai
Kouhai11mo 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
Angius11mo ago
You can always ask in more specialized channels, #gui in this case
Mekasu0124
Mekasu012411mo 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