我的数据库中有一个查找表和一个数据表.我将以性别和人为例.所以,让我们说性别表看起来像这样:
Id Code
1 Male
2 Female
Run Code Online (Sandbox Code Playgroud)
而person表看起来像这样:
Id Name GenderId
1 Bob 1
2 Jane 2
Run Code Online (Sandbox Code Playgroud)
我首先在EF代码中为这两个表建模,如下所示:
public class Gender
{
public int Id {get;set;}
public string Code {get;set;}
}
public class Person
{
public int Id {get;set;}
public string Name {get;set;}
public int GenderId {get;set;}
public virtual Gender {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如果我读了一个已经在数据库中的人,那么我可以访问person.Gender.Code没有问题.如果我这样做:
var person = new Person
{
Name = "Bob",
GenderId = 1,
};
context.People.Add(person);
context.SaveChanges();
var code = person.Gender.Code;
Run Code Online (Sandbox Code Playgroud)
然后它会正确保存但在最后一行会失败,因为性别为空.如果我然后打开一个新的上下文并加载保存的实体,那么最后一行工作正常.是否有一种方法可以在保存后直接访问性别,就好像我刚从数据库加载实体一样?
我有一个如下定义的类:
[XmlRoot("ClassName")]
public class ClassName_0
{
//stuff...
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个ClassName_0列表,如下所示:
var myListInstance= new List<ClassName_0>();
Run Code Online (Sandbox Code Playgroud)
这是我用来序列化的代码:
var ser = new XmlSerializer(typeof(List<ClassName_0>));
ser.Serialize(aWriterStream, myListInstance);
Run Code Online (Sandbox Code Playgroud)
这是我用来反序列化的代码:
var ser = new XmlSerializer(typeof(List<ClassName_0>));
var wrapper = ser.Deserialize(new StringReader(xml));
Run Code Online (Sandbox Code Playgroud)
如果我将它序列化为xml,则生成的xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassName_0 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ClassName_0>
<stuff></stuff>
</ClassName_0>
<ClassName_0>
<stuff></stuff>
</ClassName_0>
</ArrayOfClassName_0>
Run Code Online (Sandbox Code Playgroud)
有没有办法序列化并能够从/向ClassName_0列表反序列化以下内容?
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassName xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ClassName>
<stuff></stuff>
</ClassName>
<ClassName>
<stuff></stuff>
</ClassName>
</ArrayOfClassName>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在app.xaml中为组合框定义了一个全局样式,如下所示:
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid Width="{TemplateBinding Width}">
<ToggleButton
Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press">
</ToggleButton>
<ContentPresenter
Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup
Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid
Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding …
Run Code Online (Sandbox Code Playgroud) 我正在尝试按照以下说明调用Google的OAuth2身份验证服务:https://developers.google.com/accounts/docs/OAuth2ForDevices
我将所有必需的参数放入查询字符串并发送请求.这适用于"获取用户代码"部分,但不适用于"获取访问和刷新令牌"部分.
经过多次游戏并获得400 Bad Request错误后,我发现,您可以使用FormUrlEncodedContent创建请求,而不是将数据放入查询字符串中,并通过application\x-www-form-urlencoded将内容作为内容发送内容类型.
这是以前的代码:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(fullUrl);
Run Code Online (Sandbox Code Playgroud)
其中fullUrl是这样的:
https://accounts.google.com/o/oauth2/device/code?client_id=812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
Run Code Online (Sandbox Code Playgroud)
新代码是:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(url);
requestMessage.Content = new FormUrlEncodedContent(CreateDictionary(queryStringNames, queryStringValues));
Run Code Online (Sandbox Code Playgroud)
网址是:
https://accounts.google.com/o/oauth2/device/code
Run Code Online (Sandbox Code Playgroud)
queryStringNames和queryStringValues是所需参数的名称和值的字符串数组.
这两种方法有什么区别?是否可以安全地假设所有POST调用都可以使用URL编码内容请求而不是将数据放入查询字符串中?
我有一个带有一堆值的组合框.我想在选择"列表"时更改组合框的宽度.这是我目前的xaml:
<ComboBox Name="DataTypeSelector" ItemsSource="{Binding ElementName=DataItemsBuildWindow, Path=DataContext.Types}" SelectedValue="{Binding DataType}" HorizontalAlignment="Stretch" Width="160">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Value="List" Binding="{Binding SelectedValue, ElementName=DataTypeSelector, Converter={StaticResource ToStringConverter}}">
<Setter Property="Width" Value="80" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这不起作用.
如果我将Setter更改为:
<Setter Property="Visibility" Value="Hidden" />
Run Code Online (Sandbox Code Playgroud)
然后在选择"列表"时隐藏组合框.为什么它不适合宽度?