小编Tor*_*enJ的帖子

反序列化KeyValuePair <string,string> Json.Net

我需要string像这样反序列化

{
  "example": {
    "id": "12345",
    "name": "blabla"
  }
}

变成一个KeyValuePair<string, string>类似的东西.

我试过了:

var pair = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(d["example"].ToString()); 
Run Code Online (Sandbox Code Playgroud)

(d["example"]返回如上所示的json字符串)

结果是空的KeyValuePair<string, string>.

有什么方法可以解决这个问题吗?

c# json.net deserialization

6
推荐指数
1
解决办法
8323
查看次数

实例化时调用类方法?

我不知道如何更好地描述它,所以标题可能有点令人困惑.

我想知道是否可以通过在实例化时使用... = new MyClass()和调用此类的非静态方法来实例化一个类?

例如,类似的东西:

return new MyClass().SetName("some name");
Run Code Online (Sandbox Code Playgroud)

我想我以前见过类似的东西,但我找不到它.做一些像...这样的事情有点烦人

MyClass myclass = new MyClass();
myclass.SetName("some name");
return myclass;
Run Code Online (Sandbox Code Playgroud)

有没有办法缩短它或像我的第一个例子那样做?(请不要建议我使用属性而不是SetName(string)- 这只是一个例子)

提前致谢!

c#

6
推荐指数
4
解决办法
8076
查看次数

MultiTrigger:条件绑定到DependencyProperty不起作用

我有一个MainMenuButton类型的自定义按钮UserControl和atm我正在设计它.现在我想实现一个MultiTrigger只在满足两个条件时才改变按钮的外观.

第一个条件是if IsMouseOver == true.我简单地说如下Condition:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
    </MultiTrigger.Conditions>
    <MultiTrigger.EnterActions>
        <Setter TargetName="LayoutRoot" Property="Background" Value="Red">
    </MultiTrigger.EnterActions>
    <MultiTrigger.ExitActions>
        <Setter TargetName="LayoutRoot" Property="Background" Value="Black">
    </MultiTrigger.ExitActions>
</MultiTrigger>
Run Code Online (Sandbox Code Playgroud)

第二个条件与a DependencyProperty:

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainMenuButton), new PropertyMetadata(false));
Run Code Online (Sandbox Code Playgroud)

在另一个SO帖子中,用户说我可以DataTrigger用来做出反应IsCheckedProperty.所以我尝试了其他帖子的代码,但它不起作用:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="False"/>
    </MultiTrigger.Conditions>
    <MultiTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource MouseEnter}"/>
    </MultiTrigger.EnterActions>
    <MultiTrigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource MouseLeave}"/>
    </MultiTrigger.ExitActions>
</MultiTrigger>
Run Code Online (Sandbox Code Playgroud)

怎么解决这个问题?谢谢你的回答!:)

c# wpf dependency-properties multitrigger

5
推荐指数
1
解决办法
7960
查看次数

JQuery:$(window).resize()无效

我正在尝试在页面加载时手动触发$(window).resize()方法.我创建了一个函数来在页面上垂直和水平居中div元素

$(window).resize(function(){
    $('.pagecentered').css({
        position: 'absolute',
        left: ($(window).width() - $('.pagecentered').outerWidth())/2,
        top: ($(window).height() - $('.pagecentered').outerHeight())/2
    });
});
Run Code Online (Sandbox Code Playgroud)

在这个功能之后我打电话

$(window).resize();
Run Code Online (Sandbox Code Playgroud)

手动触发resize()事件.

不幸的是它不起作用.在重新加载时,div不按预期居中.如果我调整浏览器窗口的大小,div元素居中,它只会在页面加载时触发.

有什么方法可以解决这个问题吗?我使用的是Chrome 27.0.1453.116 m(最新版本的atm)和JQuery 1.10.2

jquery google-chrome

5
推荐指数
1
解决办法
1万
查看次数

如何将MainWindow.xaml拆分为单独的文件? - <Style>的外包?

嗨,我想知道如何将我的MainWindow.xaml分成不同的xaml文件?我想把我的款式外包,并在我需要的时候包括它们.我搜索了一些解决方案,发现了一些stackoverflow帖子,但没有一个帮助我.

我想实现这样的东西(伪代码)

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApp"
    Title="MyApp" Height="350" Width="525">
<Window.Resources>    
    //Import external xaml file with textbox style here 
    //instead of:
<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        //add code here
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
    <TextBox Width="60"/>
    <Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

3
推荐指数
1
解决办法
949
查看次数

连接到不存在的mongodb服务器不会引发异常

我正在玩Java的MongoDB驱动程序.所以我刚刚创建了一个简单的应用程序来连接到MongoDB服务器并选择一个数据库.
所以我创建了一个实例MongoClient并选择了一个'DB':

try
{
    MongoClient client = new MongoClient("localhost", 27017);
    DB database = client.getDB("example");
}catch(Exception e){
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

由于mongod我的机器上没有正在运行的实例,我预计client会抛出一个Exception.不幸的是情况并非如此.
即使选择数据库也没有任何反应.它就像有一个正在运行的mongod实例一样.

我查看了有关Java驱动程序的文档,但找不到任何有关它的信息.与Google相同.
我错过了什么吗?

我正在使用官方网站上的最新MongoDB驱动程序(版本2.12.2).

java mongodb mongodb-java

3
推荐指数
1
解决办法
2093
查看次数

如何使用有时相同的条目对List <Person>进行排序?

我有一个关于在C#中排序的问题.

让我们假设有List<Person> personList50个项目.
每个人Person都有string forename, surname.

现在我想对这份人员名单进行排序.
首先,列表应按照排序Forename.
因此我会用这个:
personList.Sort((p1, p2)=>string.Compare(p1.Forename, p2.Forname, true));

在此之后,我想Forname用他们的相同的方式对所有条目进行排序Surname.

我怎样才能做到这一点?

编辑:@Russ Cam:这是一个示例列表.

未排序:

David Johnson
William Black
David Smith
Matthew Edwards
Jayden Anderson
Andrew Connor
Adam Johnson
Daniel Armstrong
Steve Anderson
Daniel Black
Run Code Online (Sandbox Code Playgroud)

排序方式:

Adam Johnson
Andrew Connor
Daniel Armstrong
Daniel Black
David Johnson
David Smith
Jayden Anderson
Matthew Edwards
Steve Anderson
William Black
Run Code Online (Sandbox Code Playgroud)

c# sorting wpf list

2
推荐指数
1
解决办法
185
查看次数

ListView:自定义datatemplate中的拉伸网格到最大宽度?

我想DataTemplate为我创建一个自定义ListView.我有一个简单的class Person:

public class Person
{
    public string Name{get;set;}
    public SolidColorBrush SomeColor{get;set;}

    public Person(string name){ Name = name; }
}
Run Code Online (Sandbox Code Playgroud)

我将ItemsSourcemy 的属性设置ListView为a List<Person> persons.如果我只是使用DisplayMemberPath=Name一切正常,但我想有一个自定义模板来显示NameSomeColor.我创建了一个示例Grid来展示我希望它看起来如何(没有任何绑定,仅作为示例):

<Grid Height="50">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto" MinWidth="10"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Patrick" Grid.Column="0" TextAlignment="Right" Margin="0,0,10,0" VerticalAlignment="Center"/>
    <Rectangle Fill="Green" Grid.Column="1" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

这是我尝试实现我DataTemplateListView:

<ListView x:Name="listView" ItemsSource="{Binding ElementName=pageRoot, Path=FriendList ,Mode=OneWay}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalAlignment" Value="Stretch"/> …
Run Code Online (Sandbox Code Playgroud)

c# windows-runtime winrt-xaml windows-store-apps

2
推荐指数
1
解决办法
2519
查看次数

使用模板模板参数的模板参数

我现在正在玩templateC++中的s并且一直坚持下去template template parameters.

假设我有以下课程:

template<typename T>
struct MyInterface
{
    virtual T Foo() = 0;
}

class MyImpl : public MyInterface<int>
{
public:
    int Foo() { /*...*/ }
};

template< template<typename T> typename ImplType>
class MyHub
{
public:
    static T Foo()
    {
        ImplType i;
        return i.Foo();
    }

private:
    MyHub() { }
    ~MyHub() { }
};
Run Code Online (Sandbox Code Playgroud)

从本质上讲,我希望有一个接受实现的static class类似MyHub,MyInterface并提供某些static方法来使用它们static T Foo().

然后我试着用MyHub:

int main()
{
    int i …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17

2
推荐指数
1
解决办法
190
查看次数

世博会开始后崩溃

使用启动我的React Native项目时遇到问题expo start
浏览器打开并显示以下页面:
世博浏览器

大约一秒钟后,页面完全变暗,并且控制台(已EXPO_DEBUG=true设置)中出现以下错误:

Expo DevTools is running at http://localhost:19002
Opening DevTools in the browser... (press shift-d to disable)
error Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class. Run CLI with --verbose flag for more details.

Metro Bundler process exited with code 1
Error: Metro Bundler process exited with code 1
    at ChildProcess.<anonymous> (C:\@expo\xdl@56.2.8\src\Project.ts:1804:16)
    at Object.onceWrapper (events.js:300:26)
    at ChildProcess.emit (events.js:210:5)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about …
Run Code Online (Sandbox Code Playgroud)

node.js reactjs react-native expo

2
推荐指数
1
解决办法
2180
查看次数

使用指定的 ControlTemplate 设置 UserControl 的填充?

我最近创建了一个UserControl用于WrapPanel可视化一些数据的工具。我Padding对其应用了 a 以便在每个元素之间留出一些空间。

第一个版本如下所示:

<UserControl x:Class="IFCS.EntityOverviewPanel"
         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" 
         xmlns:l="clr-namespace:IFCS"
         mc:Ignorable="d"
         Padding="5, 5, 5, 5"
         d:DesignHeight="300" d:DesignWidth="300">
    <!-- Code -->
</UserControl>
Run Code Online (Sandbox Code Playgroud)

现在我只是应用了 aControlTemplate来覆盖我的Padding设置。

当前版本如下所示:

<UserControl x:Class="IFCS.EntityOverviewPanel"
         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" 
         xmlns:l="clr-namespace:IFCS"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <!-- Code -->
        </ControlTemplate>
    </UserControl.Template>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

我想再次申请PaddingUserControl但我尝试的一切都不起作用。我尝试应用一个Style使用

<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Padding" Value="5, 5, 5, 5"/>
    </Style>
</UserControl.Style>
Run Code Online (Sandbox Code Playgroud)

但这没有用。在“标题”中设置Padding也不起作用。

我必须在哪里设置该Padding值才能获得与第一个版本相同的结果?

c# wpf controltemplate

1
推荐指数
1
解决办法
1822
查看次数