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

[Bug Report]: MasaBlazor.BreakpointChanged触发过于频繁 #2067

Closed
Yu-Core opened this issue Jul 25, 2024 · 4 comments · Fixed by #2068
Closed

[Bug Report]: MasaBlazor.BreakpointChanged触发过于频繁 #2067

Yu-Core opened this issue Jul 25, 2024 · 4 comments · Fixed by #2068
Labels
feature/breakpoint type/bug Something isn't working
Milestone

Comments

@Yu-Core
Copy link
Contributor

Yu-Core commented Jul 25, 2024

Masa.Blazor version

1.6.4

Describe the bug

按理说应该只在断点改变时才会触发
但实际上,只要窗口宽度或高度改变,哪怕是同一断点,依然被触发
https://github.com/masastack/MASA.Blazor/blob/main/src/Masa.Blazor/Services/Breakpoint/Breakpoint.cs#L125

Expected Behavior

No response

Steps To Reproduce

No response

Reproduction code

No response

.NET version

No response

What browsers are you seeing the problem on?

No response

Additional context

No response

@capdiem
Copy link
Contributor

capdiem commented Jul 25, 2024

@Yu-Core 你需要确切地知道从什么断点到什么断点吗?你是通过inject MasaBlazor还是inject Breakpoint来使用的?

@Yu-Core
Copy link
Contributor Author

Yu-Core commented Jul 25, 2024

inject MasaBlazor
比较需要类似于MobileChanged的XsChanged、SmAndDownChanged...,这样可以更精细的控制
我目前的写法是这么写的

public class MasaBlazorHelper
{
    readonly MasaBlazor _masaBlazor;

    Breakpoint _previousBreakpoint = default!;
    Breakpoints _previousBreakpointName;

    public Breakpoint Breakpoint => _masaBlazor.Breakpoint;

    public event EventHandler<MyBreakpointChangedEventArgs>? BreakpointChanged;

    public MasaBlazorHelper(MasaBlazor masaBlazor)
    {
        _masaBlazor = masaBlazor;
        _masaBlazor.BreakpointChanged += MasaBlazorBreakpointChanged;
        UpdatePreviousBreakpoint();
    }

    private void MasaBlazorBreakpointChanged(object? sender, BreakpointChangedEventArgs e)
    {
        if (_previousBreakpointName == Breakpoint.Name)
        {
            return;
        }

        _previousBreakpointName = Breakpoint.Name;
        var eventArgs = new MyBreakpointChangedEventArgs()
        {
            XsChanged = _previousBreakpoint.Xs != Breakpoint.Xs,
            SmChanged = _previousBreakpoint.Sm != Breakpoint.Sm,
            MdChanged = _previousBreakpoint.Md != Breakpoint.Md,
            LgChanged = _previousBreakpoint.Lg != Breakpoint.Lg,
            XlChanged = _previousBreakpoint.Xl != Breakpoint.Xl,
            SmAndDownChanged = _previousBreakpoint.SmAndDown != Breakpoint.SmAndDown,
            SmAndUpChanged = _previousBreakpoint.SmAndUp != Breakpoint.SmAndUp,
            MdAndDownChanged = _previousBreakpoint.MdAndDown != Breakpoint.MdAndDown,
            MdAndUpChanged = _previousBreakpoint.MdAndUp != Breakpoint.MdAndUp,
            LgAndDownChanged = _previousBreakpoint.LgAndDown != Breakpoint.LgAndDown,
            LgAndUpChanged = _previousBreakpoint.LgAndUp != Breakpoint.LgAndUp,
        };

        UpdatePreviousBreakpoint();
        BreakpointChanged?.Invoke(this, eventArgs);
    }

    private void UpdatePreviousBreakpoint()
    {
        _previousBreakpoint = Breakpoint.ShallowClone();
    }
}

@capdiem
Copy link
Contributor

capdiem commented Jul 25, 2024

@Yu-Core SmAndUpChanged 这些比较繁琐,只提供一个断点也行吧

你的写法:

private void MasaBlazorOnBreakpointChanged(object? sender, MyBreakpointChangedEventArgs e)
{
    if (e.SmAndUpChanged)
    {
        InvokeAsync(StateHasChanged);
    }
}

新支持的写法:

private void MasaBlazorOnBreakpointChanged(object? sender, BreakpointChangedEventArgs e)
{
    // e.Breakpoint 为当前断点 Xs,Sm,Md,Lg,Xl 其中之一
    if (e.Breakpoint > Breakpoints.Xs)
    {
        InvokeAsync(StateHasChanged);
    }
}

@Yu-Core
Copy link
Contributor Author

Yu-Core commented Jul 25, 2024

有道理,还是你的写法更合理一些

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/breakpoint type/bug Something isn't working
Projects
None yet
2 participants