[UWP] [VB]使用.NET Native Toolchain直接访问ComboboxItem内容编译导致运行时。 [英] [UWP][VB]Accessing ComboboxItem content directly with .NET Native Toolchain Compilation results in a run time.

查看:55
本文介绍了[UWP] [VB]使用.NET Native Toolchain直接访问ComboboxItem内容编译导致运行时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


尝试直接访问组合框的内容虽然Items集合导致Windows.UI.Xaml中出现未处理的异常。


使用Visual Studio 2015 Update 3复制:


1创建一个空白通用Windows应用程序(我使用Visual Basic)


2。在表单和按钮中添加一个combobox1。添加一个组合框。我做了一个项目"What the Heck"

< Page 
x:Class =" App1.MainPage"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local =" using:App1"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable =" d">

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< ComboBox x:Name =" comboBox1"的Horizo​​ntalAlignment = QUOT;左" VerticalAlignment = QUOT;陀螺"宽度= QUOT; 120">
< ComboBoxItem Content =" Heck" />
< / ComboBox>
<按钮x:名称="按钮"含量="按钮和QUOT;的Horizo​​ntalAlignment = QUOT;左"高度= QUOT; 61"余量= QUOT; 114,234,0,0" VerticalAlignment = QUOT;陀螺"宽度= QUOT; 119" />

< / Grid>
< / Page>

3。对于后面的代码添加以下内容:

 Private Sub button_Click(sender As Object,e As RoutedEventArgs)Handles button.Click 
Dim foo As ComboBoxItem = comboBox1.Items.Item(0)'ok
Dim foostring As String = foo.Content.ToString'ok
foostring = comboBox1.Items.Item(0).Content.ToString'kaboom
foo = comboBox1.Items.SingleOrDefault(Function(x)x.Content.ToString =" What the Heck")'kaboom
foo = SearchForItem(comboBox1," What the Heck")'ok
End Sub

函数SearchForItem(ByVal cb As ComboBox,ByVal searchString As String)As ComboBoxItem
For Each cbitem As ComboBoxItem in cb.Items
if cbitem.Content.ToString = searchString然后返回cbitem
下一个
返回Nothing
结束函数

这一行"foostring = comboBox1.Items.Item(0).Content.ToString""如果在.NET Native Toolchain编译中运行,则下一行将导致未处理的异常。






解决方案

嗨SonofSmog,


欢迎
到开发通用Windows应用论坛!
 请使用  标记 
发布到此论坛时,谢谢!


您可以指示的对象类型运行.Net Native Toolchain编译时的代码。使用以下代码替换"kaboom"的代码,

 foostring = DirectCast(comboBox1.Items。 Item(0),ComboBoxItem).Content.ToString()
foo = comboBox1.Items.SingleOrDefault(Function(x)DirectCast(x,ComboBoxItem).Content.ToString =" What the Heck")




祝你好运,


Breeze Liu


Attempting to directly access the content of comboboxitem though the Items collection results in a Unhandled exception in Windows.UI.Xaml.

To duplicate using Visual Studio 2015 Update 3:

1 Create a Blank Universal Windows App (I used Visual Basic)

2. Add a combobox1 to the form and a button. Add one comboboxitem. I made the item "What the Heck"

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
            <ComboBoxItem Content="What the Heck"/>
        </ComboBox>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="61" Margin="114,234,0,0" VerticalAlignment="Top" Width="119"/>

    </Grid>
</Page>

3. For the code behind add the following:

Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        Dim foo As ComboBoxItem = comboBox1.Items.Item(0)                   'ok
        Dim foostring As String = foo.Content.ToString                      'ok
        foostring = comboBox1.Items.Item(0).Content.ToString                'kaboom
        foo = comboBox1.Items.SingleOrDefault(Function(x) x.Content.ToString = "What the Heck") ' kaboom
        foo = SearchForItem(comboBox1, "What the Heck")                     'ok
    End Sub

    Function SearchForItem(ByVal cb As ComboBox, ByVal searchString As String) As ComboBoxItem
        For Each cbitem As ComboBoxItem In cb.Items
            If cbitem.Content.ToString = searchString Then Return cbitem
        Next
        Return Nothing
    End Function

This line "foostring = comboBox1.Items.Item(0).Content.ToString" and the next line will result in a unhandled exception if you run in .NET Native Toolchain compilation.



解决方案

Hi SonofSmog,

Welcome to the Developing Universal Windows apps forum! Please utilize tagging when posting to this forum, thanks!

You can indicate the object type of the Item in your code when you run in .Net Native Toolchain compilation. Using the following code to replace the code of "kaboom",

        foostring = DirectCast(comboBox1.Items.Item(0), ComboBoxItem).Content.ToString()
        foo = comboBox1.Items.SingleOrDefault(Function(x) DirectCast(x, ComboBoxItem).Content.ToString = "What the Heck")


Best regards,

Breeze Liu


这篇关于[UWP] [VB]使用.NET Native Toolchain直接访问ComboboxItem内容编译导致运行时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆