Abstract UserControl inheritance in Visual Studio designer

前端 未结 9 1429
广开言路
广开言路 2020-12-01 12:13
abstract class CustomControl : UserControl 
{
    protected abstract int DoStuff();
}

class DetailControl : CustomControl
{
    protected override int DoStuff()
            


        
9条回答
  •  孤街浪徒
    2020-12-01 12:18

    I was new to this in UWP and it was driving me nuts. I didn't think of an abstract base class for a UserControl. I went in a different direction. I created a non-xaml Helper class ... HBase. Each View, say VContract, had a corresponding Helper called HContract. All the speciality code for each view was lodged there. The conversations that would have been between the ViewModel VMContract and View VContract now pass through HContract. We can enforce how HWhatever behaves with IHBase. It's not truly an answer to the OP's question, but does show an alternative approach. All the Views are now basically shells. Whether you x:Bind to the VContract or HContract is a decision for you to make. I chose the VContract way and in the end I think that was a mistake.

    The UWP problem with exceptions in Design Mode is now easily fixed with:

    if (false == Windows.ApplicationModel.DesignMode.DesignModeEnabled)
    {
                HContract = new HContract(this);
    //   Put code here that fails in Design mode but must at run time               
    }
    

提交回复
热议问题