Avalonia Unable to cast object exception

I have created what amounts to a single page application with Avalonia. In this application, layout like this : layout, As shown in the img , on the left is NavigationView, and on the right is the Frame Control. I would like to use Frame Control to display those two pages(home , tools).

code like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <ui:NavigationView PaneDisplayMode="Left" IsBackButtonVisible="False"
IsSettingsVisible="False"
IsPaneToggleButtonVisible="False"
Width="80"
ItemInvoked="NavView_OnItemInvoked"
Name = "NavView">
<ui:NavigationView.MenuItems >
<ui:NavigationViewItem Content="Home"
Tag="Home"
SelectsOnInvoked="True"
></ui:NavigationViewItem>
<ui:NavigationViewItem Content="tools"
Tag="Tools"
SelectsOnInvoked="True"
></ui:NavigationViewItem>
</ui:NavigationView.MenuItems>
<ui:Frame Name="Frame"></ui:Frame>
</ui:NavigationView>
<ContentControl Grid.Column="1" Margin="10,10,0,0"
CornerRadius="50"
>
<ui:Frame Name="Frame2" Navigated="Frame2_OnNavigated" ></ui:Frame>
</ContentControl>
</code>
<code> <ui:NavigationView PaneDisplayMode="Left" IsBackButtonVisible="False" IsSettingsVisible="False" IsPaneToggleButtonVisible="False" Width="80" ItemInvoked="NavView_OnItemInvoked" Name = "NavView"> <ui:NavigationView.MenuItems > <ui:NavigationViewItem Content="Home" Tag="Home" SelectsOnInvoked="True" ></ui:NavigationViewItem> <ui:NavigationViewItem Content="tools" Tag="Tools" SelectsOnInvoked="True" ></ui:NavigationViewItem> </ui:NavigationView.MenuItems> <ui:Frame Name="Frame"></ui:Frame> </ui:NavigationView> <ContentControl Grid.Column="1" Margin="10,10,0,0" CornerRadius="50" > <ui:Frame Name="Frame2" Navigated="Frame2_OnNavigated" ></ui:Frame> </ContentControl> </code>
                    <ui:NavigationView PaneDisplayMode="Left" IsBackButtonVisible="False"
                                       IsSettingsVisible="False"
                                       IsPaneToggleButtonVisible="False"
                                       Width="80"
                                       ItemInvoked="NavView_OnItemInvoked"
                                       Name = "NavView">
                        
                        <ui:NavigationView.MenuItems >
                            <ui:NavigationViewItem Content="Home"
                                                    Tag="Home"
                                                   SelectsOnInvoked="True"
                                                   ></ui:NavigationViewItem>
                            <ui:NavigationViewItem Content="tools"
                                Tag="Tools"
                                                   SelectsOnInvoked="True"
                            ></ui:NavigationViewItem>
                        </ui:NavigationView.MenuItems>
                        
                        <ui:Frame Name="Frame"></ui:Frame>
                    </ui:NavigationView>


                <ContentControl Grid.Column="1" Margin="10,10,0,0"
                                CornerRadius="50"
                                >
                    <ui:Frame Name="Frame2" Navigated="Frame2_OnNavigated" ></ui:Frame>
                </ContentControl>

And I also do somethings on subpage what display on Frame Controls

Such as HomePage(HomeView),I have placed two controls in this view, one is TextBlock and the other is a Button,

I have Binding both of these two Controls

this is Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <Grid ShowGridLines="True">
<TextBlock Width="300" Background = "Red" Text="{Binding GTER, FallbackValue='null'}"></TextBlock>
<Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding ButtonAction}">名字</Button>
</Grid>
</code>
<code> <Grid ShowGridLines="True"> <TextBlock Width="300" Background = "Red" Text="{Binding GTER, FallbackValue='null'}"></TextBlock> <Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding ButtonAction}">名字</Button> </Grid> </code>
    <Grid ShowGridLines="True">
        <TextBlock Width="300" Background = "Red" Text="{Binding GTER, FallbackValue='null'}"></TextBlock>
        <Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding ButtonAction}">名字</Button>
    </Grid>

When i execute it , A ERROR HAS BEEN THROWN!

ERROR LIKE THIS

System.InvalidCastException: Unable to cast object of type 'APP.ViewModels.MainWindowViewModel' to type 'APP.ViewModels.HomeViewModel

I have found a solution on GitHub Link

I followed his advice and modified my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <TextBlock Width="300" Background = "Red" Text="{Binding $parent.((vm:HomeViewModel)DataContext).GTER, FallbackValue='null'}"></TextBlock>
<Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding $parent.((vm:HomeViewModel)DataContext).ButtonAction">名字</Button>
</code>
<code> <TextBlock Width="300" Background = "Red" Text="{Binding $parent.((vm:HomeViewModel)DataContext).GTER, FallbackValue='null'}"></TextBlock> <Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding $parent.((vm:HomeViewModel)DataContext).ButtonAction">名字</Button> </code>
        <TextBlock Width="300" Background = "Red" Text="{Binding $parent.((vm:HomeViewModel)DataContext).GTER, FallbackValue='null'}"></TextBlock>
        <Button Background = "Transparent" BorderBrush = "Transparent" Command="{Binding $parent.((vm:HomeViewModel)DataContext).ButtonAction">名字</Button>

BUT IT DID NOT WORK!

TextBlock always display null instead of the Binding value

Button also becomes unClickable

Please forgive me for my poor English. Anybody give me a clue as to what I am doing wrong? 🙁

New contributor

lou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật