0

In my code, I have an UIElement variable I set with certain button presses. Now, I have this variable:

public UIElement currentMenu;

Set to this value:

currentMenu = (UIElement)Resources["Home"];

I get it from the Resources so I don't have to manage it messily in the code-behind, I will export the Resources to a seperate ResourceDictionary once I get this problem solved.

My SplitView looks like this:

<SplitView x:Name="NavPane" OpenPaneLength="250" CompactPaneLength="50" Content="{x:Bind currentMenu}" DisplayMode="CompactOverlay" IsPaneOpen="False" PaneClosing="NavPane_PaneClosing">

The prblem comes in at this point, the Binding crashes the entire application with an unhndled win32 exception. I get no description and the error code changes every time. I have checked with break points whether this behaviour is actually caused by the binding, and it is.

If you have any suggestions on what might be going wrong here, please post an answer. I will supply any additional information needed (if reasonable, I'm not going to send you my entire project files)

Any help is appreciated!

StepUp
  • 36,391
  • 15
  • 88
  • 148
Light Drake
  • 121
  • 3
  • 9

2 Answers2

0

Your problem that you are using a variable, not a property.

private UIElement currentMenu;
public string CurrentMenu
{
   get { return currentMenu; }
   set { 
         currentMenu=value);
         OnPropertyChanged("CurrentMenu");
       }
} 

So the basic rules to bind Control to a "varaible":

  • Variable should be a property, not a field.
  • it should be public.
  • Either a notifying property (suitable for model classes) or a dependency property (suitable for view classes)

To notify UI you should implement INotifyPropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Update:

Your Bidning should looks like:

<SplitView x:Name="NavPane" OpenPaneLength="250" CompactPaneLength="50" 
       Content="{Binding CurrentMenu}" DisplayMode="CompactOverlay" IsPaneOpen="False"
       PaneClosing="NavPane_PaneClosing">
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • I now have this implementation: `private UIElement currentMenu; public string CurrentMenu { get { return currentMenu.ToString(); } set { currentMenu = (UIElement) Resources[value]; OnPropertyChanged("CurrentMenu"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }` Still returns simliar errors – Light Drake May 16 '16 at 17:53
  • @LightDrake could you post what innerexception says and full stack exception trace. – StepUp May 16 '16 at 19:11
  • @LightDrake is it UWP? – StepUp May 16 '16 at 19:26
  • Yes, it is UWP, but I'm not sure how to access the error message, what I get (upon building) is: An unhandled win32 exception just occured in [projectname].exe [11576] <- This changes every time – Light Drake May 16 '16 at 19:32
  • @LightDrake Read this http://stackoverflow.com/questions/5620678/nullreferenceexception-no-stack-trace-where-to-start and this http://stackoverflow.com/questions/20492739/how-to-show-a-stack-trace-reports-of-unhandled-exceptions-in-wpf – StepUp May 16 '16 at 19:37
  • I'll get the stack trace and then get back to you here :) – Light Drake May 16 '16 at 19:51
  • The problem persists, my application is also giving me the message "A debugger is attached to [application] but not configured to debug this unhandled exception. To debug this exception, detach the current debugger" This also means that I have no chance to even access the stack trace since the debuggers don't seem to want to debug. My own code is never called on ("not configured to handle...") and Visual Studio doesn't seem to feel like doing anything either. – Light Drake May 17 '16 at 17:01
  • @LightDrake if there is an exception, if you remove the following code `Content="{x:Bind currentMenu}"`? – StepUp May 18 '16 at 09:56
  • If I remove the binding, the exception is gone as well. If I try a different type of binding, namely {Binding CurrentMenu}, nothing happens at all. – Light Drake May 18 '16 at 17:30
  • @LightDrake **namely {Binding CurrentMenu}, nothing happens at all cause** `DataContext` is not set. Good article about `Binding, DataContext` in UWP can be seen by the following address https://msdn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth?f=255&MSPPError=-2147217396 – StepUp May 18 '16 at 18:06
  • I have set the DataContext since the beginning of this discussion. I have changed the implementation somewhat, but am still getting the same error: private UIElement currentMenu; public UIElement CurrentMenu { get { return currentMenu; } } public void CurrentSet(string value) { currentMenu = (UIElement)Resources[value]; OnPropertyChanged("CurrentMenu"); } and: {x:Bind Path=CurrentMenu} – Light Drake May 18 '16 at 18:55
  • @LightDrake could you show code how `DataContext` is set? Could you test that `DataContext` works properly? – StepUp May 18 '16 at 20:47
  • In the constructor of the Page in the Code-behind, there is a simple `this.DataContext = this` It works properly, I tested that, but as soon as I try to set the SplitView.Content to anything by Resources or Data Bindings, everything breaks apart. Is this an error concerning the "Content" member? Or are the UIElements I put into the Resources corrupted in some manner? I really have no clue. How do other people manage different pages? Maybe I'm going for the incorrect solution here... – Light Drake May 19 '16 at 17:40
  • I couldn't test it until now, but it still crashes everything. I think I broke my Data Bindings :( – Light Drake May 23 '16 at 17:08
0

I have found the answer to my question!

Namely, this was not the way to do it. Instead, I declared a Frame inside the Content of the SplitView:

<SplitView.Content>
    <Frame x:Name="activeMenu"/>
</SplitView.Content>

Then, I use the Frame.Navigate() function to load my menus into the Frame:

    public MainPage()
    {
        DataContext = this;
        this.InitializeComponent();
        SetMenu(0);
    }

    private void SetMenu(int key)
    {
        switch (key)
        {
            case 0:
                activeMenu.Navigate(typeof(HomeMenu));
                break;
            //You can add in as many cases as you need
        }
    }

What you then need is to set all the menus you want as separate Pages within your project files. in this example, HomeMenu.xaml contains the grid for the menu people see upon starting up the app.

This solved the problem, but thank you to everyone (StepUp) who contributed to the original (unfortunately unsuccessful) solution!

Light Drake
  • 121
  • 3
  • 9
  • http://meta.stackexchange.com/questions/126180/is-it-acceptable-to-write-a-thank-you-in-a-comment – StepUp May 25 '16 at 08:48