Skip to content

I aimed for .cs files. Not enough experienced for .xaml files. - #4085

Merged
Keboo merged 1 commit into
MaterialDesignInXAML:masterfrom
Markot4:master
Aug 28, 2026
Merged

I aimed for .cs files. Not enough experienced for .xaml files.#4085
Keboo merged 1 commit into
MaterialDesignInXAML:masterfrom
Markot4:master

Conversation

@Markot4

@Markot4 Markot4 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Two things I have to write before I specify what I did:

  1. I apologize if I broke any rules when it comes to contribution.

  2. I did use GitHub CoPilot in certain cases since I do not want to mess up the source code even though I forked it.

Anyways: here is what I did:

  1. I fixed the invalid cast error in src/MahMaterialDragablzMashUp/DialogsViewModel.cs:
  • Replaced the direct cast ((MainWindow)Application.Current.MainWindow) with a safe lookup:
  • var leftFlyout = Application.Current?.MainWindow?.FindName("LeftFlyout") as Flyout;
  • Toggle leftFlyout.IsOpen if found.
  • This avoids the compile-time CS0030 error (Window -> MainWindow) and works regardless of XAML-generated partial class details.

I find it funny how this removed 4 errors and many warnings.

  1. I made the two changes to address the warnings in DialogsViewModel.cs:
  • Marked DialogDictionary as readonly.
  • Changed ProgressDialog from async void to async Task.
  1. I updated the property setter in MahVieModel.cs to throw an ArgumentOutOfRangeException instead of System.Exception, which static analyzers (S112) flag as a poor practice. Property setters should signal invalid argument values with a more specific exception type.

I replaced throw new Exception("Value must be positive") with throw new System.ArgumentOutOfRangeException(nameof(UpDownValue), value, "Value must be non-negative"), which provides a specific exception type, the offending parameter name, and the actual value.

  1. Since there weren't any other .cs files, I decided to edit DialogsViewModel.cs:
  • Changed InputDialog signature to: private async Task InputDialog() and used await DialogCoordinator.Instance.ShowInputAsync(...).

  • Changed the command registrations in the constructor to async lambdas that await the methods:

  • ShowInputDialogCommand = new AnotherCommandImplementation(async _ => await InputDialog());

  • ShowProgressDialogCommand = new AnotherCommandImplementation(async _ => await ProgressDialog());

These make the async calls observed (awaited) and remove CS4014 warnings.

I have no intention of editing .xaml files since I have only studied XML, but not quite enough to understand what I can do.
If there is anything else I have to do, make sure to comment and specify eveything else I can fix. Or even add.

Edit: Oh, wait. I forgot I needed to close VS after installing one of NuGet packages... I will have to commit again just in case.

Edit edit: I saw errors for xaml files just to disappear. what?

1. I apologize if I broke any rules when it comes to contribution.

2. I did use GitHub CoPilot in certain cases since I do not want to mess up the source code even though I forked it.

Anyways: here is what I did:

1. I fixed the invalid cast error in src/MahMaterialDragablzMashUp/DialogsViewModel.cs:

* Replaced the direct cast ((MainWindow)Application.Current.MainWindow) with a safe lookup:
 * var leftFlyout = Application.Current?.MainWindow?.FindName("LeftFlyout") as Flyout;
 * Toggle leftFlyout.IsOpen if found.
 * This avoids the compile-time CS0030 error (Window -> MainWindow) and works regardless of XAML-generated partial class details.

I find it funny how this removed 4 errors and many warnings.

2. I made the two changes to address the warnings in DialogsViewModel.cs:
 * Marked DialogDictionary as readonly.
 * Changed ProgressDialog from async void to async Task.

3. I updated the property setter in MahVieModel.cs to throw an ArgumentOutOfRangeException instead of System.Exception, which static analyzers (S112) flag as a poor practice. Property setters should signal invalid argument values with a more specific exception type.

I replaced throw new Exception("Value must be positive") with throw new System.ArgumentOutOfRangeException(nameof(UpDownValue), value, "Value must be non-negative"), which provides a specific exception type, the offending parameter name, and the actual value.

4. Since there weren't any other .cs files, I decided to edit DialogsViewModel.cs:

1. Changed InputDialog signature to: private async Task InputDialog() and used await DialogCoordinator.Instance.ShowInputAsync(...).

2. Changed the command registrations in the constructor to async lambdas that await the methods:
 * ShowInputDialogCommand = new AnotherCommandImplementation(async _ => await InputDialog());
 * ShowProgressDialogCommand = new AnotherCommandImplementation(async _ => await ProgressDialog());

These make the async calls observed (awaited) and remove CS4014 warnings.

I have no intention of editing .xaml files since I have only studied XML, but not quite enough to understand what I can do.
@corvinsz

corvinsz commented Aug 24, 2026

Copy link
Copy Markdown
Member

@Markot4 thank you for your contribution.

A more descriptive PR title would be helpful. 😉

Regarding the changes themselves: if the goal is to refactor the MahAppsDragablzDemo application (which I'm not sure is actively maintained anymore), it may be worth considering a broader refactor of the entire class and adopting CommunityToolkit.Mvvm where appropriate. It's one of the most widely used MVVM frameworks in the .NET ecosystem and would provide a more consistent approach throughout the code.

If your primary motivation is to use dialogs and drawers (flyouts) with data binding support, you may also want to consider using this library (MaterialDesignInXaml), which provides built-in support for these scenarios. I'm not particularly familiar with the MahApps controls, as I haven't worked with them extensively.

Oh and also:

Anyways: here is what I did:
I fixed the invalid cast error in src/MahMaterialDragablzMashUp/DialogsViewModel.cs:

I'm not sure which error. The app compiles and runs fine for me 🤔

@Keboo Keboo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this.

@Keboo
Keboo enabled auto-merge (squash) August 28, 2026 05:54
@Keboo
Keboo merged commit 47ea955 into MaterialDesignInXAML:master Aug 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants