3
votes

I wrote simple XAML file:

<Window      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="400" d:DesignWidth="250"
             WindowStartupLocation="CenterScreen"
             ResizeMode="NoResize" Title="Окошко" Width="250" Height="400">
    <Grid>
        <!--Настраиваем сетку-->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!--Теперь по сетке раскидываем контролы-->
        <GroupBox x:Name="listTitle" x:FieldModifier="public" Header="Список элементов:" Margin="5"
                  Padding="5">
            <!--В объекте ListBox указываем, что источником данных для элементов
            списка следует использовать свойство DataContext родительского элемента-->
            <ListBox x:Name="listbox" x:FieldModifier="public"
                     ItemsSource="{Binding}" SelectionMode="Multiple">
                <!--Шаблон отображения элементов списка-->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--Указываем, что каждый элемент следует отображать
                        в виде текста, который должен считываться из свойства "Value"-->
                        <TextBlock Text="{Binding Path=Value}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>
        <GroupBox Header="Примечание:" Grid.Row="1" Margin="5" Padding="5">
            <TextBlock x:Name="txtNotes" x:FieldModifier="public" TextWrapping="Wrap"/>
        </GroupBox>
        <StackPanel Orientation="Horizontal" Grid.Row="2"
                    HorizontalAlignment="Right">
            <Button x:Name="btnAccept" x:FieldModifier="public" Margin="5"
                    Padding="5" IsDefault="True">Принять</Button>
            <Button x:Name="btnExit" x:FieldModifier="public" Margin="5"
                    Padding="5" IsCancel="True">Выход</Button>
        </StackPanel>
    </Grid>
</Window>

It is simple window:

enter image description here

I parse this in my code:

using (io.FileStream fs = new io.FileStream(fileFullName, io.FileMode.Open)) {
    depObj = XamlReader.Load(fs) as DependencyObject;
    fs.Close();
}

It works fine in .NET 4.0, but in .NET 3.5 SP1 I get exception:

System.Windows.Markup.XamlParseException occurred
Message=The attribute 'FieldModifier' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml'. Line '17' Position '38'.

Why does this happen?

1

1 Answers

4
votes

Well, it shouldn't work in .NET 4 either, but apparently the parser is being "nice" to you by ignoring your error of using x:FieldModifier.

According to the MSDN documentation, then you must also have an x:Class directive.

If a XAML production uses x:FieldModifier anywhere, the root element of that XAML production must declare an x:Class Directive.

Using the x:FieldModifier makes no sense if you're not declaring a class/codebehind file and are loading the XAML dynamically.