Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot to get CascadingParam when naviated from INavigation.PushAsync<>() #90

Closed
le-nn opened this issue Dec 7, 2022 · 5 comments · Fixed by #106
Closed

Cannot to get CascadingParam when naviated from INavigation.PushAsync<>() #90

le-nn opened this issue Dec 7, 2022 · 5 comments · Fixed by #106

Comments

@le-nn
Copy link

le-nn commented Dec 7, 2022

Cannot to get CascadingParam when naviated from INavigation.PushAsync<>()

public record Theme {
    public string Background { get; init; } = ""#ffffffff";
}

AppShell

<CascadingValue Value="Theme">
    <Shell FlyoutIsPresented="true">
        <ShellContent Title="Navigation">
            <PageNavigatedFrom />
        </ShellContent>
        <ShellContent Title="Navigation">
            <PageNavigatedTo />
        </ShellContent>
    </Shell>
</CascadingValue>

@code {
    Theme Theme { get; } = new();
}

PageNavigateFrom

@inject INavigation Navigation

<ContentPage Padding="12"
             Title="">
    <Button Text="Navigate" OnClick="HandleNavigate" />
</ContentPage>

@code {
    [CascadingParameter]
    Theme? Theme { get; set; }

    void HandleNavigate() {
        Navigation.PushAsync<PageNavigatedTo>();
    }
}

PageNavigateTo

<ContentPage Padding="12"
             Title="">
    <Label Text="@Theme?.Background.ToString()" />
</ContentPage>

@code {
    [CascadingParameter]
    Theme? Theme { get; set; } // Always null
}
@Dreamescaper
Copy link
Owner

Dreamescaper commented Dec 7, 2022

That's an interesting use case.

Currently Navigation.PushAsync is equivalent to smth like that from Maui: Navigation.PushAsync(new PageNavigatedTo()), i.e. it always creates a new page, in a separate blazor render tree. It doesn't re-use <ShellContent Title="Navigation"> <PageNavigatedTo /> </ShellContent> in your Shell file.

I'll take a look whether it is possible to re-use cascading values from calling component (I doubt it that Blazor allows that, but not sure).

I'm thinking about your use case though. Would it work if you register Theme as a singleton in DI container instead of CascadingValue?

@le-nn
Copy link
Author

le-nn commented Dec 7, 2022

In my usecase,
If registering Singleton in DI instead of Cascading Param,
I should handle StatehasChanged manually in every component with something like IObservable.
So, I need CascadingValue. (ex: Changing the App Theme)

When route from nav menu button in Shell with ShellContent , it worked fine.

@Dreamescaper
Copy link
Owner

I see. Technically it is possible to handle StateHasChanged in root component only, but again - PushAsync currently creates a separate blazor tree, therefore there is no shared root component.

@le-nn
Copy link
Author

le-nn commented Dec 8, 2022

I will deal with using a state management library like Fluxor.
Thanks.

@Dreamescaper
Copy link
Owner

Dreamescaper commented Jan 18, 2023

@le-nn
I'm thinking to add a support for layout components. While it doesn't make much sense to define markup there, but it might be useful for cascading values or error boundaries.

Do you think it would make sense from the perspective of this request?

OTOH, since adding a markup in a layout doesn't make any sense (and probably won't work at all), maybe it makes sense to create a similar concept with a different name to avoid confusion.

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 a pull request may close this issue.

2 participants