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

Experiencing "Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to MyComponent" #8931

Closed
MarkStega opened this issue Jul 10, 2023 · 12 comments
Labels
area-compiler Umbrella for all compiler issues bug Something isn't working

Comments

@MarkStega
Copy link

MarkStega commented Jul 10, 2023

@jjonescz @danroth27

I ran into the issue reported in #8761 which was closed with a reference to issue (#8718) which was closed as complete.

I was using the 8.0.preview.5 SDK. I followed the table reference for 8.0.100-preview.6.23330.14 SDK, installed, and still have the error.

In my component MOIPrimaryTreatment.Razor I have another component defined as:

<TreatmentDialog pTreatmentDialogCompleted="@TreatmentDialogCompletionAsync"
@ref="pTreatmentDialogRef" />

Instead of having a component defined in the corresponding razor.g.cs I have an element:

            __builder.OpenElement(21, "TreatmentDialog");
            __builder.AddAttribute(22, "pTreatmentDialogCompleted", 
#nullable restore
#line 28 "C:\Solutions\OHI\Optimiser.2021\Optimiser.Client\Views\Dashboard\Processes\Oncology\Segments\Infusion\Helpers\MOIPrimaryTreatment.razor"
                                             TreatmentDialogCompletionAsync

#line default
#line hidden
#nullable disable
            );
            __builder.AddElementReferenceCapture(23, (__value) => {
#nullable restore
#line 29 "C:\Solutions\OHI\Optimiser.2021\Optimiser.Client\Views\Dashboard\Processes\Oncology\Segments\Infusion\Helpers\MOIPrimaryTreatment.razor"
      pTreatmentDialogRef = __value;

#line default
#line hidden
#nullable disable
            }
            );
            __builder.CloseElement();

And then, of course, when I try to use the component reference I get the "Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to TreatmentDialog"

Other similar embedded components are working as expected. I just don't see the code pattern that is causing the generation to fail.

[Edit] I also tried sdk 7.0.400-preview.23356.7 and have the same error...

@ghost ghost added the untriaged label Jul 10, 2023
@MarkStega MarkStega changed the title Experiencing Experiencing "Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to MyComponent" Jul 10, 2023
@davidwengier davidwengier added the area-compiler Umbrella for all compiler issues label Jul 11, 2023
@jjonescz
Copy link
Member

Thanks for reporting this. Both 8.0.100-preview.6.23330.14 and 7.0.400-preview.23356.7 should have the fix. Can you please share more details about the component that's giving the error (TreatmentDialog)? Is it defined across multiple files (like a .razor and a .cs file), or what is special about it? Alternatively you can share source code of a minimal reproducible example, so I can take a look.

@ghost ghost removed the untriaged label Jul 11, 2023
@MarkStega
Copy link
Author

@jjonescz Here are the two files; If you don't see anything that could be triggering the error I can try to extract to a minimal repo. But there is a ton backing components/etc. that will make it difficult. I have followed this pattern throughout the application and have not run into this issue before.

The 'TreatmentDialog is pretty straightforward with a razor & razor.cs file:

razor:

@namespace Optimiser.Views

<GCDialog pDialogTitle="@pTitle"
          pOnDialogActionAsync="OnDialogComplete"
          pButtonSelection="eDialogButtonSelections.OKCancel"
          @ref="@pGCDialogReference">

    <pBody>
        <div class="flow-flex-column">
            @if (pShowNewTX)
            {
                <FlowDivider pAdditionalStyles="margin-top: .75em; "
                             pTitle="Search"
                             pDividerStyle="eFlowDividerStyle.Small" />

                <GCTextBox pAdditionalStyles="margin-top: .75em; width: 500px; "
                           pValue="@pSearchText"
                           pValueChanged="@SearchTextChanged" />

                <GCSelect @bind-pValue="@pSearchCriteria"
                          pDensity="eDensity.Minus4"
                          pGCItemValidation="eItemValidation.DefaultToFirst"
                          pGCItems="@pSearchCriteriaItems"
                          pGCSelectInputStyle="eSelectInputStyle.Outlined"
                          pGCTextAlignStyle="eTextAlignStyle.Left" />

                <GCGrid pColumnConfigurations="@pColumnConfigurationsNewTX"
                        pGroupedOrderedData="@pGroupedOrderedDataNewTX"
                        pKeyExpression="@(pTX => pTX.GUID)"
                        pLogIdentification="MOI Predefined Tx"
                        pMeasurement="MB_Grid_Measurement.FitToData"
                        pOnMouseClickCallback="@MouseClickAsync"
                        TRowData="@MedOncInf_TxDetail_DD" />

            }
        </div>
    </pBody>

</GCDialog>

and the cs file:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using GeneralComponents;

using Material.Blazor;

using Microsoft.AspNetCore.Components;

using Optimiser.DataTier.DataDefinitions;
using Optimiser.Infrastructure.MVVMFramework.Collections;
using Optimiser.Infrastructure.MVVMFramework.Components;
using Optimiser.Infrastructure.MVVMFramework.Converters;
using Optimiser.SharedUtilities;
using Optimiser.ViewModels.CachedEntities;
using Optimiser.ViewModels.Process;

using static Optimiser.SharedUtilities.MedOncInf_TxDetail_DD;

//
//  2017-10-06  Mark Stega
//              Created
//
//  2018-05-25  Mark Stega
//              Added support for procedures
//
//  2018-07-19  Mark Stega
//              Added support for searching therapy names
//
//  2018-07-31  Mark Stega
//              Added search parameters of "Contains" and "StartsWith"
//
//  2023-07-09  Mark Stega
//              Created from Treatment.xaml & Treatment.xaml.cs from O.2012
//

namespace Optimiser.Views;

public partial class TreatmentDialog

{
    #region Members

    [Parameter] public EventCallback<string> pTreatmentDialogCompleted { get; set; }

    [Inject] private CachedEntitiesViewModel pCachedEntitiesVM { get; set; }
    [Inject] private ProcessViewModel pProcessVM { get; set; }

    private List<MBGridColumnConfiguration<MedOncInf_TxDetail_DD>> pColumnConfigurationsNewTX { get; set; } = null;
    private MedOncInf_TxDetail_DD pCurrentTx { get; set; }
    private GCDialog pGCDialogReference { get; set; }
    private IEnumerable<KeyValuePair<string, IEnumerable<KeyValuePair<string, MedOncInf_TxDetail_DD>>>> pGroupedOrderedDataNewTX { get; set; }
    public SortableObservableCollection<MedOncInfDefinition_DD> pInsCo1Collection { get; set; }
    public SortableObservableCollection<MedOncInfDefinition_DD> pInsCo2Collection { get; set; }
    private bool pShowNewTX { get; set; }
    private List<GCSingleSelectListElement<string>> pSearchCriteriaItems { get; set; }
    private string pSearchText { get; set; }
    public MedOncInfDefinition_DD pSelectedInsCo1 { get; set; }
    public MedOncInfDefinition_DD pSelectedInsCo2 { get; set; }
    public MedOncInfDefinition_DD pSelectedSourceTx { get; set; }
    public SortableObservableCollection<MedOncInfDefinition_DD> pSourceFilteredTxCollection { get; set; }
    public SortableObservableCollection<MedOncInfDefinition_DD> pSourceUnfilteredTxCollection { get; set; }
    private string pTitle { get; set; }

    private string m_SearchCriteria;
    public string pSearchCriteria
    {
        get { return m_SearchCriteria; }
        set
        {
            if (value != m_SearchCriteria)
            {
                m_SearchCriteria = value;
                ApplySearchCriteria();
            }
        }
    }

    #endregion

    #region ctor

    public TreatmentDialog()
    {
        pSearchCriteriaItems = new();
        pSearchCriteriaItems.Add(new GCSingleSelectListElement<string>() { pLabel = "Contains", pDataValue = "Contains" });
        pSearchCriteriaItems.Add(new GCSingleSelectListElement<string>() { pLabel = "Starts with", pDataValue = "Starts with" });

        pColumnConfigurationsNewTX = new();

        pColumnConfigurationsNewTX.Add(new MBGridColumnConfiguration<MedOncInf_TxDetail_DD>(
            columnType: MB_Grid_ColumnType.Icon,
            dataExpression: c => new ConvertBoolToMBGridIconSpecification().Convert(c.Active),
            title: "Active"));
        pColumnConfigurationsNewTX.Add(new MBGridColumnConfiguration<MedOncInf_TxDetail_DD>(
            dataExpression: c => c.Descriptor1,
            title: "Mode"));
        pColumnConfigurationsNewTX.Add(new MBGridColumnConfiguration<MedOncInf_TxDetail_DD>(
            dataExpression: c => c.Descriptor2,
            title: "Tx"));
        pColumnConfigurationsNewTX.Add(new MBGridColumnConfiguration<MedOncInf_TxDetail_DD>(
            dataExpression: c => c.Descriptor3,
            title: "Agent"));
    }

    #endregion

    #region ApplySearchCriteria

    private void ApplySearchCriteria()
    {
        pSourceFilteredTxCollection = new();
        var contains = pSearchCriteria == "Contains";
        var startsWith = pSearchCriteria == "Starts with";

        foreach (var tx in pSourceUnfilteredTxCollection)
        {
            if (   (pSearchText.Length == 0)
                || (contains && tx.Descriptor1.IndexOf(pSearchText) >= 0)
                || (startsWith && tx.Descriptor1.IndexOf(pSearchText) == 0))
            {
                pSourceFilteredTxCollection.Add(tx);
            }
        }

    }

    #endregion

    #region OpenDialogAsync
    public async Task OpenDialogAsync()
    {
        await pGCDialogReference.OpenDialogAsync();
    }
    #endregion

    #region CloseDialogAsync
    public async Task CloseDialogAsync()
    {
        await pGCDialogReference.CloseDialogAsync();
    }
    #endregion

    #region MouseClickAsync
    protected async Task MouseClickAsync(string uid)
    {
        await Task.CompletedTask;

//        MedOncInf_TxDetail_DD primaryTx = new();

//        foreach (var ptx in pProcessVM.pMedOncInf_TxDetailCollection_PrimaryTreatments)
//        {
//            if (ptx.GUID.ToString() == uid)
//            {
//                // We have the matching treatment
//                primaryTx = ptx;
//                break;
//            }
//        }
//        // edit the treatment
//#if (MSH)
//        pTreatmentDialogRef.SetDTO(
//            p_CurrentTx: primaryTx,
//            p_IsNew: false);
//#endif
//#if (DEMO || DEVELOP || LBH)
//        pTreatmentDialogRef.SetDTO(
//            p_CurrentTx: primaryTx,
//            p_IsNew: false);
//#endif
//        await pTreatmentDialogRef.OpenDialogAsync();
    }
    #endregion

    #region OnDialogComplete
    // Set as the click handler on our OK and Cancel buttons
    protected async Task OnDialogComplete(string buttonId)
    {
        // Post our selection event
        await pTreatmentDialogCompleted.InvokeAsync(buttonId);
    }
    #endregion

    #region Properties corresponding to MedOncInf_TxDetail_DD

    public bool pActive { get; set; }
    public DateTime pAppointmentDateTime { get; set; }
    public string pDescriptor1 { get; set; }
    public string pDescriptor2 { get; set; }
    public string pDescriptor3 { get; set; }
    private bool pNoAuthRequired { get; set; }
    public string pInsuranceCompany1 { get; set; }
    public string pInsuranceAuthorization1 { get; set; }
    public string pInsuranceNote1 { get; set; }
    public string pInsuranceCompany2 { get; set; }
    public string pInsuranceAuthorization2 { get; set; }
    public string pInsuranceNote2 { get; set; }
    public DateTime pValidFrom { get; set; }
    public DateTime pValidTo { get; set; }
    #endregion

    #region SearchTextChanged

    private void SearchTextChanged(string newValue)
    {
        pSearchText = newValue;
        ApplySearchCriteria();
    }

    #endregion

    #region SetDTO
    public void SetDTO(
            MedOncInf_TxDetail_DD p_CurrentTx,
            bool p_IsNew = false,
            bool p_ShowInsurance = false)

    {
        pCurrentTx = p_CurrentTx;

        if (p_IsNew)
        {
            pShowNewTX = true;
            pTitle = "New TX";

            pSourceUnfilteredTxCollection = new();
            foreach (MedOncInfDefinition_DD tx in pCachedEntitiesVM.pMedOncInfDefinitionList)
            {
                if (((int)pCurrentTx.TxClass == tx.DefinitionClass)
                    && ((int)pCurrentTx.ProcessTypeIdentifier == tx.DefinitionType)
                    && tx.Active)
                {
                    pSourceUnfilteredTxCollection.Add(tx);
                }
            }

            pSearchText = "";
        }
        else
        {
            pShowNewTX = false;
            pTitle = "Edit TX";
        }

        var sourceInsCoCollection = new SortableObservableCollection<MedOncInfDefinition_DD>();

        if (((pCurrentTx.TxClass == eMedOncInfDefinitionClass.PrimaryTx)
                || (pCurrentTx.TxClass == eMedOncInfDefinitionClass.SupportiveTx)
                || (pCurrentTx.TxClass == eMedOncInfDefinitionClass.Procedure))
            && p_ShowInsurance)
        {
            foreach (MedOncInfDefinition_DD tx in pCachedEntitiesVM.pMedOncInfDefinitionList)
            {
                if (((int)eMedOncInfDefinitionClass.InsuranceCompany == tx.DefinitionClass)
                    && ((int)pCurrentTx.ProcessTypeIdentifier == tx.DefinitionType)
                    && tx.Active)
                {
                    sourceInsCoCollection.Add(tx);
                }
                pInsCo1Collection = sourceInsCoCollection;
                pInsCo2Collection = sourceInsCoCollection;
            }
        }

    }

    #endregion

}

@ghost ghost added untriaged and removed needs more info labels Jul 11, 2023
@jjonescz
Copy link
Member

Sorry, don't see anything that would suggest why the error happens. If you cannot create a minimal repro and your whole solution is easy to build, you can share it privately via VS Feedback (post a link to the ticket here if you do that).

@MarkStega
Copy link
Author

@jjonescz Thanks for looking at the code; I will create as small a repository as I can. Building is simple as all you need to do is to load the solution in Visual Studio & then F6...

@ghost ghost added untriaged and removed needs more info labels Jul 11, 2023
@MarkStega
Copy link
Author

@jjonescz -- I ripped apart the solution removing all servers, etc. I zipped the source of the client and it exceeds what I can upload here (File size too big: 25 MB are allowed, 37 MB were attempted to upload.)

I can either split the source and do two zips or I can create a private github repository and give you read access. Or, if you have another solution, I am happy to hear that also.

@jjonescz
Copy link
Member

Have you removed the bin and obj folders with binaries? I wouldn't expect source code itself be that big. And have you tried uploading via Developer Community portal? Private repo would also work.

@MarkStega
Copy link
Author

MarkStega commented Jul 11, 2023

It's a BIG application. Yes, the binaries were removed (at the root of the project there is a file called superclean.cmd that is used to remove artifacts but it looks like it doesn't work for ".artifacts", but that only has 3 Mb).

Anyhow, the community portal upload worked: https://developercommunity.visualstudio.com/t/Razor-compilation-failure/10412528

I get the same failure:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0029	Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to 'Optimiser.Views.TreatmentDialog'	Optimiser.Client	C:\Solutions\OHI\Optimiser.2021.Client\Optimiser.Client\Views\Dashboard\Processes\Oncology\Segments\Infusion\Helpers\MOIPrimaryTreatment.razor	29	Active

@MarkStega
Copy link
Author

BTW, I am using the 'DevServer' configuration and the 8.0.0-preview.6.23330.14 sdk

@ScarletKuro
Copy link

ScarletKuro commented Jul 11, 2023

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0029	Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to 'Optimiser.Views.TreatmentDialog'	Optimiser.Client	C:\Solutions\OHI\Optimiser.2021.Client\Optimiser.Client\Views\Dashboard\Processes\Oncology\Segments\Infusion\Helpers\MOIPrimaryTreatment.razor	29	Active

Hi. This is just a guess, but could the issue be related to long paths? I've come across similar situations in the past when people have shared their reproduction code with me. What if you try moving the TreatmentDialog.razor file up one level? Perhaps then your MOIPrimaryTreatment will be able to "recognize" it?

@jaredpar jaredpar added bug Something isn't working and removed untriaged labels Jul 11, 2023
@ghost ghost added the untriaged label Jul 11, 2023
@jjonescz
Copy link
Member

@MarkStega the problem is in your Optimiser.Client.csproj - you have <Content Remove= items on some razor files, including the TreatmentDialog.razor (and also below similar <None Include= items). That makes the files invisible to Razor compiler.

@ghost ghost removed the untriaged label Jul 12, 2023
@MarkStega
Copy link
Author

@jjonescz

Thanks for finding that -- It is a very annoying Visual Studio 'side effect' to put the '''<Content remove``` items if you copy/rename files. It does so silently. I have often copied an existing component in VS, pasted it, renamed the "- Copy".

I am going to post this in VS feedback as a bug unless you think this is a good behavior on the part of VS.

@MarkStega
Copy link
Author

@ScarletKuro See above for the root cause. The uneven support for long file names in VS is an issue but not the problem this time. I can't open the generated files with VS due to the path being too long so I have to copy them to a shorter path.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-compiler Umbrella for all compiler issues bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants