Click here to Skip to main content
15,881,413 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: c# Spotify API not returning correctly. Pin
Pete O'Hanlon4-Jun-23 8:42
mvePete O'Hanlon4-Jun-23 8:42 
AnswerRe: c# Spotify API not returning correctly. Pin
jschell5-Jun-23 5:01
jschell5-Jun-23 5:01 
QuestionNeed help please regarding your awesome WPF MultiComboBox project :) Pin
Member 1160463425-May-23 5:21
Member 1160463425-May-23 5:21 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
jeron125-May-23 5:50
jeron125-May-23 5:50 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
Richard Deeming25-May-23 21:28
mveRichard Deeming25-May-23 21:28 
QuestionNavigationControl - Part 3 Pin
Kevin Marois14-May-23 8:01
professionalKevin Marois14-May-23 8:01 
AnswerRe: NavigationControl - Part 3 Pin
Richard Deeming14-May-23 21:40
mveRichard Deeming14-May-23 21:40 
GeneralRe: NavigationControl - Part 3 Pin
Kevin Marois15-May-23 17:14
professionalKevin Marois15-May-23 17:14 
Thanks, I understand the change you recommened, but it doesn't seem to change anything. I have two different issues. I beleive I solved the first one.
  1. The Projects pane should be expanded on startup BEFORE it loads so the user seees the wait indicator. This bit of code in the MainWindow simulates storing and resetting it from settings later on. if there was an ExpandedExpanded (past-tense) then I could call Load() from there.
Main Window.cs
NavigationPaneInfos = new List<NavigationPaneModel>
{
    new NavigationPaneModel
    {
        Header = "Projects", 
        NavigationItemType = NavigationItemType.Project, 
        DataSource = Functional.Apply(Repository.GetNavigationItems, NavigationItemType.Project),
        IsExpanded = true   // <====== 1. This should force the Projects pane to expand on startup, and that should trigger Load() after the wait indicator is visible
    },

    new NavigationPaneModel
    {
        Header = "Inventory", 
        NavigationItemType = NavigationItemType.Inventory,
        DataSource = Functional.Apply(Repository.GetNavigationItems, NavigationItemType.Inventory),
    },

    new NavigationPaneModel
    {
        Header = "Companies" , 
        NavigationItemType = NavigationItemType.Company,
        DataSource = Functional.Apply(Repository.GetNavigationItems, NavigationItemType.Company),
    },

    new NavigationPaneModel
    {
        Header = "Employees", 
        NavigationItemType = NavigationItemType.Employee,
        DataSource = Functional.Apply(Repository.GetNavigationItems, NavigationItemType.Employee),
    }
};
so, that caused this
private static object CoerceIsPaneExpanded(DependencyObject d, object baseValue)
{
    var control = (NavigationPane)d;
    var newVal = control._isInitialized ? baseValue : (object)false;
    return newVal; //<======= THIS IS NEVER TRUE
}
and
private void NavigationPane_Initialized(object? sender, EventArgs e)
{
    _isInitialized = true;
    CoerceValue(IsPaneExpandedProperty);

    if (_isPaneExpanded) //<====== 2. THIS IS NEVER TRUE
    {
        _ = Load();
    }
}
so, I added this, which correctly sets _isPaneExpanded based off the value set in the main window
private static async void OnNavigationPaneModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = (NavigationPane)d;
    var model = e.NewValue as NavigationPaneModel;
    if (model != null)
    {
        control._isPaneExpanded = model.IsExpanded;
    }
}

This is where the UI is freezing up
private async Task Load()
{
    if (NavigationPaneModel != null && NavigationPaneModel.DataSource != null)
    {
        // 2. This calls the repo and returns data, but it's not async, which is 
        // causing the UI to frees
        var dataSource = NavigationPaneModel.DataSource(); 

        List<NavigationEntity>? data = null;

        if (dataSource != null)
        {
            data = await Task.Run(() => dataSource);
        }

        if (data != null)
        {
            Items = new ObservableCollection<NavigationEntity>(data);
        }
    }
}
so I tried this but....
private async Task Load()
{
    if (NavigationPaneModel != null && NavigationPaneModel.DataSource != null)
    {
        List<NavigationEntity>? data = null;

        await Task.Run(() => 
        {
            // This throws with
            //
            //  "The calling thread cannot access this object because a different thread owns it.'"
            //
            // So I need to figure out how to call the delegate async
            data = NavigationPaneModel.DataSource();
        });

        if (data != null)
        {
            Items = new ObservableCollection<NavigationEntity>(data);
        }
    }
}
So, it seems like all that's left is to get the backend call to fire async. The delegate was set in the UI using
NavigationPaneModel.DataSource = Functional.Apply(Repository.GetNavigationItems, NavigationItemType.Project)
How can I invoke the delegate async from Load()?

I checked my code into the repo.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

QuestionBubbling Event Question Pin
Kevin Marois9-May-23 14:32
professionalKevin Marois9-May-23 14:32 
AnswerRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 5:28
mveGerry Schmitz12-May-23 5:28 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 5:35
professionalKevin Marois12-May-23 5:35 
GeneralRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 5:46
mveGerry Schmitz12-May-23 5:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 6:16
professionalKevin Marois12-May-23 6:16 
AnswerRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 9:55
mvaKenneth Haugland14-May-23 9:55 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 10:40
professionalKevin Marois14-May-23 10:40 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 11:06
mvaKenneth Haugland14-May-23 11:06 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 11:08
professionalKevin Marois14-May-23 11:08 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 11:19
mvaKenneth Haugland14-May-23 11:19 
GeneralRe: Bubbling Event Question Pin
Kevin Marois15-May-23 17:47
professionalKevin Marois15-May-23 17:47 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland15-May-23 18:46
mvaKenneth Haugland15-May-23 18:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois16-May-23 7:02
professionalKevin Marois16-May-23 7:02 
QuestionExpander Header Content Stretch Pin
Kevin Marois9-May-23 11:25
professionalKevin Marois9-May-23 11:25 
AnswerRe: Expander Header Content Stretch Pin
Richard Deeming9-May-23 21:48
mveRichard Deeming9-May-23 21:48 
GeneralRe: Expander Header Content Stretch Pin
Kevin Marois13-May-23 6:05
professionalKevin Marois13-May-23 6:05 
QuestionVB.NET WPF drag and drop for LISTBOX and TREEVIEWs..Anyone help with examples? Pin
Member 84579549-May-23 8:51
Member 84579549-May-23 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.