组件在Blazor.wasm应用中无法显示

huangapple go评论63阅读模式
英文:

Component Wont display in Blazor.wasm app

问题

我正在尝试创建一个Blazor Web Assembly界面来管理数据源。我试图加载一个自定义的编辑表单,它将以模态方式显示在Telerik数据网格上。

Telerik Grid正常显示一切,如果我将其恢复为默认的弹出式编辑器,那么它也可以正常工作,但对于我的需求来说,它就无法正常工作。

我尝试了各种方法,但就是无法让这个弹出式编辑器正常工作。

非常感谢任何帮助!

<strong> Home.razor </strong> <br/>

// 这是我调用编辑处理程序以分配_selectedFeed的按钮
&lt;GridCommandColumn Width=&quot;120px&quot;&gt;
    &lt;GridCommandButton OnClick=&quot;@((args) =&gt; EditHandler(_selectedFeed = args.Item as MarketDataFeedsAndSources_UI))&quot; ThemeColor=&quot;@ThemeConstants.Button.ThemeColor.Primary&quot; Icon=&quot;edit&quot;&gt;编辑&lt;/GridCommandButton&gt;
    &lt;GridCommandButton Command=&quot;Delete&quot; ThemeColor=&quot;@ThemeConstants.Button.ThemeColor.Error&quot;&gt;删除&lt;/GridCommandButton&gt;
&lt;/GridCommandColumn&gt;

@if(_selectedFeed != null)
{
    &lt;FeedCustomEditor @bind-SelectedFeed=&quot;@_selectedFeed&quot;&gt;
    &lt;/FeedCustomEditor&gt;
}

<strong> Home.razor EditHandler </strong>

public MarketDataFeedsAndSources_UI _selectedFeed { get; set; }

async void EditHandler(MarketDataFeedsAndSources_UI selectedFeed)
{
    if(selectedFeed != null)
    {
        _originalFeed = selectedFeed;
        _selectedFeed = selectedFeed;
        MarketDataFeed_Details feedDetails = selectedFeed.dataFeed_Details;
        StateHasChanged();
    }
}

<strong> FeedCustomEditor.razor </strong>

&lt;TelerikWindow Visible=&quot;@(SelectedFeed != null)&quot; Modal=&quot;true&quot;&gt;

    &lt;WindowTitle&gt;
        @{
            if (SelectedFeed.FeedID &lt; 0)
            {
                &lt;strong&gt;添加数据源&lt;/strong&gt;
            }
            else
            {
                &lt;strong&gt;编辑数据源&lt;/strong&gt;
            }
        }
    &lt;/WindowTitle&gt;
    &lt;WindowContent&gt;
        @*&lt;EditForm Model=&quot;@SelectedFeed&quot; OnValidSubmit=&quot;@Save&quot;&gt;*@
        &lt;EditForm Model=&quot;@SelectedFeed&quot;&gt;
            &lt;DataAnnotationsValidator/&gt;
            &lt;div class=&quot;form-row&quot;&gt;
                &lt;div class=&quot;col&quot;&gt;
                    &lt;label&gt;
                        @*如何加载数据源到这里*@ 数据源:
                        &lt;TelerikTextBox Enabled=&quot;false&quot; @bind-Value=&quot;@SelectedFeed.dataFeed_Details.marketDataSource.name&quot;&gt;&lt;/TelerikTextBox&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/EditForm&gt;
    &lt;/WindowContent&gt;

&lt;/TelerikWindow&gt;

@code {
    [Parameter]
    public MarketDataFeedsAndSources_UI SelectedFeed { get; set; }
}

我访问的资源:<br/>
https://github.com/telerik/blazor-ui/blob/master/grid/custom-popup-form/Pages/SeparateComponent.razor <br />

https://www.telerik.com/forums/can%27t-show-window-form-different-component <br />

https://www.telerik.com/forums/custom-popup-editor-for-grid-similar-to-form-editor <br />

https://demos.telerik.com/blazor-ui/window/modal?_ga=2.69109841.1346078225.1685036867-1842103071.1681736109&amp;_gac=1.14172869.1684789516.CjwKCAjwvJyjBhApEiwAWz2nLTCT-XDW5AnIb6ltqXBzIcguteiEHupKWPH_3IUs6QaF6--2XzzuiBoCErIQAvD_BwE&amp;_gl=11523t83_gaMTg4MjEwMzA3MS4xNjgxNzM2MTA5_ga_9JSNBCSF54*MTY4NTEzMDQwMC4xOS4xLjE2ODUxMzA0ODcuNDAuMC4w <br />

https://demos.telerik.com/blazor-ui/grid/editing-popup <br />

英文:

I am trying to create a Blazor Web Assembly UI to manage data feeds. I am trying to load a custom edit form that will display modally over a Telerik Data-Grid.

The Telerik Grid displays everything just fine and if I revert it back to the default pop-up editor that works just fine, but it just doesn't work for my purposes.

I have tried various things and I just can't get this pop-up editor to work.

Any and all help is greatly appriciated!

<strong> Home.razor </strong> <br/>

// Here is the button where I call the Edit Handler to assign the _selectedFeed
            &lt;GridCommandColumn Width=&quot;120px&quot;&gt;
                &lt;GridCommandButton OnClick=&quot;@((args) =&gt; EditHandler(_selectedFeed = args.Item as MarketDataFeedsAndSources_UI))&quot; ThemeColor=&quot;@ThemeConstants.Button.ThemeColor.Primary&quot; Icon=&quot;edit&quot;&gt;Edit&lt;/GridCommandButton&gt;
                &lt;GridCommandButton Command=&quot;Delete&quot; ThemeColor=&quot;@ThemeConstants.Button.ThemeColor.Error&quot;&gt;Delete&lt;/GridCommandButton&gt;
            &lt;/GridCommandColumn&gt;

@if(_selectedFeed != null)
{
    &lt;FeedCustomEditor @bind-SelectedFeed=&quot;@_selectedFeed&quot;&gt;
    &lt;/FeedCustomEditor&gt;
}

<strong> Home.razor EditHandler </strong>

    public MarketDataFeedsAndSources_UI _selectedFeed { get; set; }

    async void EditHandler(MarketDataFeedsAndSources_UI selectedFeed)
    {

        if(selectedFeed != null)
        {
            _originalFeed = selectedFeed;
            _selectedFeed = selectedFeed;
            MarketDataFeed_Details feedDetails = selectedFeed.dataFeed_Details;


            StateHasChanged();
        }
    }

<strong> FeedCustomEditor.razor </strong>

&lt;TelerikWindow Visible=&quot;@(SelectedFeed != null)&quot; Modal=&quot;true&quot;&gt;

    &lt;WindowTitle&gt;
        @{
            if (SelectedFeed.FeedID &lt; 0)
            {
                &lt;strong&gt;Add Feed&lt;/strong&gt;
            }
            else
            {
                &lt;strong&gt;Edit Feed&lt;/strong&gt;
            }
        }
    &lt;/WindowTitle&gt;
    &lt;WindowContent&gt;
        @*&lt;EditForm Model=&quot;@SelectedFeed&quot; OnValidSubmit=&quot;@Save&quot;&gt;*@
        &lt;EditForm Model=&quot;@SelectedFeed&quot;&gt;
            &lt;DataAnnotationsValidator/&gt;
            &lt;div class=&quot;form-row&quot;&gt;
                &lt;div class=&quot;col&quot;&gt;
                    &lt;label&gt;
                        @*How to load the Data Sources into this *@
                        Data Source:
                        &lt;TelerikTextBox Enabled=&quot;false&quot; @bind-Value=&quot;@SelectedFeed.dataFeed_Details.marketDataSource.name&quot;&gt;&lt;/TelerikTextBox&gt;
                    &lt;/label&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/EditForm&gt;
    &lt;/WindowContent&gt;

&lt;/TelerikWindow&gt;

@code {
    [Parameter]
    public MarketDataFeedsAndSources_UI SelectedFeed { get; set; }

}

Resources I accessed: <br/>
https://github.com/telerik/blazor-ui/blob/master/grid/custom-popup-form/Pages/SeparateComponent.razor <br />

https://www.telerik.com/forums/can%27t-show-window-form-different-component <br />

https://www.telerik.com/forums/custom-popup-editor-for-grid-similar-to-form-editor <br />

https://demos.telerik.com/blazor-ui/window/modal?_ga=2.69109841.1346078225.1685036867-1842103071.1681736109&amp;_gac=1.14172869.1684789516.CjwKCAjwvJyjBhApEiwAWz2nLTCT-XDW5AnIb6ltqXBzIcguteiEHupKWPH_3IUs6QaF6--2XzzuiBoCErIQAvD_BwE&amp;_gl=11523t83_gaMTg0MjEwMzA3MS4xNjgxNzM2MTA5_ga_9JSNBCSF54*MTY4NTEzMDQwMC4xOS4xLjE2ODUxMzA0ODcuNDAuMC4w <br />

https://demos.telerik.com/blazor-ui/grid/editing-popup <br />

答案1

得分: 1

我不确定问题出在哪里,但你有一些可能会导致问题的编码问题。由于我的演示版本已过期,我无法访问Telerik库。

将所有代码移动到UI事件处理程序中,以简化OnClick。使用断点和更清晰的代码更容易调试。

<GridCommandButton OnClick=EditHandler ThemeColor="@ThemeConstants.Button.ThemeColor.Primary" Icon="edit">Edit</GridCommandButton>
// 我没有看到任何异步调用,所以不要调用它
private Task EditHandler(ChangeEventArgs e)
{
    var selectedFeed = args.Item as MarketDataFeedsAndSources_UI;

    if(selectedFeed is null)
      return Task.CompletedTask;

    _originalFeed = selectedFeed;
    _selectedFeed = selectedFeed;
    MarketDataFeed_Details feedDetails = selectedFeed.dataFeed_Details;

    // 不需要
    // StateHasChanged();

   return Task.CompletedTask;
}

在组件声明中,不要使用bind:我没有看到ValueChangedValueExpression的定义,因此它会给人一个错误的印象,即组件中实现了什么。你传递了一个对象引用,因此对话框中的所有编辑都将反映在原始对象中(无论是保存还是取消)。

<FeedCustomEditor SelectedFeed="_selectedFeed" />

在编辑器中,你不需要测试SelectedFeed的状态,因为你已经在父组件中进行了测试。

<TelerikWindow Visible="true" Modal="true">
英文:

I'm not sure what the problem is here, but you have a few coding issues that may cause it. I don't have access to the Telerik library as my demo version has expired!

Simplify the OnClick by moving all the code to the UI event handler. It's much easier to debug with break points and clearer code.

&lt;GridCommandButton OnClick=EditHandler ThemeColor=&quot;@ThemeConstants.Button.ThemeColor.Primary&quot; Icon=&quot;edit&quot;&gt;Edit&lt;/GridCommandButton&gt;
  // I don&#39;t see any async calls so don&#39;t call it
    private Task EditHandler(ChangeEventArgs e)
    {
        var selectedFeed = args.Item as MarketDataFeedsAndSources_UI;

        if(selectedFeed is null)
          return Task.CompletedTask;

        _originalFeed = selectedFeed;
        _selectedFeed = selectedFeed;
        MarketDataFeed_Details feedDetails = selectedFeed.dataFeed_Details;

        // Not needed
        // StateHasChanged();

       return Task.CompletedTask;
    }

In the component declaration change from using bind: I don't see the ValueChanged or ValueExpression defined so it gives the wrong impression of what is implemented in the component. You're passing in an object reference so all edits in the dialog will be reflected in the original (whether you save or abort!).

&lt;FeedCustomEditor SelectedFeed=_selectedFeed /&gt;

In the editor, you don't need to test the state of SelectedFeed, you already did it in the parent.

&lt;TelerikWindow Visible=true Modal=true&gt;

huangapple
  • 本文由 发表于 2023年6月1日 00:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定