小编Kev*_*sse的帖子

一种反转整数变量二进制值的方法

我有这个int nine = 9;二进制的整数1001.是否有一种简单的方法可以将其反转以便我可以获得0110

c#

18
推荐指数
3
解决办法
3万
查看次数

找不到内存泄漏

我一直在研究WP7应用程序,它的图像库应用程序,实现了基本的缩放和轻弹手势.

出于测试目的,我使用设置为Content的离线图像(它们的文件名已编号)编译应用程序,并通过硬编码字符串(稍后将替换)访问它们.

但后来意识到应用消耗了大量内存.我以为这是由于图像并发现了这个博客 ; 图像总是缓存.我使用博客中的代码来纠正这个问题.虽然消费率确实下降,但仍未释放内存.

为了最后的尝试,我创建了另一个带有基本功能2按钮的测试应用程序,用于图像的导航和图像控制,只是为了确保它不是我的手势代码可能是问题.

这是xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Image Grid.Row="0" x:Name="ImageHolder" Height="Auto" Width="Auto" Stretch="Uniform" Tap="image_Tap" />
    <TextBlock x:Name="MemUsage" />
    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <Button x:Name="PrevButton" Content="Prev" Width="240" Click="btnPrev_Click"/>
        <Button x:Name="NextButton" Content="Next" Width="240" Click="btnNext_Click"/>
    </StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这是.cs文件

    const int PAGE_COUNT = 42;
    int pageNum = 0;
    public MainPage()
    {
        InitializeComponent();
        RefreshImage();
    }

    private void btnPrev_Click(object sender, RoutedEventArgs e)
    {
        pageNum = (PAGE_COUNT + pageNum - 1) % PAGE_COUNT; // cycle to …
Run Code Online (Sandbox Code Playgroud)

c# memory memory-leaks image windows-phone-7

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

Reflection获取对象属性以对列表进行排序

我想通过存储在其中的对象的属性对c#中的列表进行排序.我有这个:

if (sortColumn == "Login")
{
    if (sortDir == "ASC")
    {
        filteredList.Sort((x, y) => string.Compare(x.Login, y.Login, true));
    }
    else
    {
        filteredList.Sort((x, y) => string.Compare(y.Login, x.Login, true));
    }
 }
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我想更通用,以便不必知道要排序的字段.我想过这样的事情:

//With sortColumn = "Login";
if (sortDir == "ASC")
{
    filteredList.Sort((x, y) => string.Compare(x.GetType().GetProperty(sortColumn), y.GetType().GetProperty(sortColumn), true));
}
else
{
    filteredList.Sort((x, y) => string.Compare(y.GetType().GetProperty(sortColumn), x.GetType().GetProperty(sortColumn), true));
}
Run Code Online (Sandbox Code Playgroud)

显然这不起作用,但这就是我想要的.有可能吗?

谢谢.

c# sorting reflection

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

隔离存储中的CreateFile的IsolatedStorageFileStream上不允许操作

我正在尝试使用以下代码在隔离存储中创建一个文件,

IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication();
storageFile.CreateFile("Html\\index.html");
Run Code Online (Sandbox Code Playgroud)

但是我在做同样的事情时会遇到异常.

System.IO.IsolatedStorage.IsolatedStorageException:IsolatedStorageFileStream上不允许操作

除此操作外,不执行任何操作.

c# isolatedstorage isolatedstoragefile windows-phone windows-phone-8

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

在自定义控件上使用StringFormat绑定

我正在尝试在WPF应用程序中使用自定义控件,并且使用StringFormat绑定时遇到一些问题.

这个问题很容易重现.首先,让我们创建一个WPF应用程序并将其命名为"TemplateBindingTest".在那里,添加一个只有一个属性(Text)的自定义ViewModel,并将其分配给Window的DataContext.将Text属性设置为"Hello World!".

现在,向解决方案添加自定义控件.自定义控件尽可能简单:

using System.Windows;
using System.Windows.Controls;

namespace TemplateBindingTest
{
    public class CustomControl : Control
    {
        static CustomControl()
        {
            TextProperty = DependencyProperty.Register(
                "Text",
                typeof(object),
                typeof(CustomControl),
                new FrameworkPropertyMetadata(null));

            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
        }

        public static DependencyProperty TextProperty;

        public object Text
        {
            get
            {
                return this.GetValue(TextProperty);
            }

            set
            {
                SetValue(TextProperty, value);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将自定义控件添加到解决方案时,Visual Studio会自动创建一个带有generic.xaml文件的Themes文件夹.我们将控件的默认样式放在那里:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TemplateBindingTest">

    <Style TargetType="{x:Type local:CustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl}">
                    <TextBlock Text="{TemplateBinding Text}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

现在,只需将控件添加到窗口,并使用StringFormat在Text属性上设置绑定.还要添加一个简单的TextBlock以确保绑定语法正确:

<Window x:Class="TemplateBindingTest.MainWindow" …
Run Code Online (Sandbox Code Playgroud)

c# wpf string-formatting templatebinding

5
推荐指数
2
解决办法
4856
查看次数

获取当前的Windows Phone 7的IP

我正在构建一个Windows Phone 7.1应用程序.我需要知道手机是否连接到任何Wifi,如果是,它在本地网络中的当前IP是什么(即192.168.0.100这样).我一直试图找出这些信息一段时间了.请帮忙.

我已经设法通过使用以下代码在我的控制台应用程序上获取本地IP

public void ScanIP()
{                
    IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            String localIP = ip.ToString();
            Console.WriteLine(localIP);                        
        }
    }

    Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)

但是,我需要为Windows Mobile 7 app做类似的事情.任何的想法 ?请分享.

windows-phone-7 c#-4.0 windows-phone-7.1 windows-phone-8

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

为什么枚举空数组不在堆上分配?

考虑以下基准:

[MemoryDiagnoser]
public class EnumerableBenchmark
{
    private IEnumerable<string> _emptyArray = new string[0];
    private IEnumerable<string> _notEmptyArray = new string[1];

    [Benchmark]
    public IEnumerator<string> ArrayEmpty()
    {
        return _emptyArray.GetEnumerator();
    }

    [Benchmark]
    public IEnumerator<string> ArrayNotEmpty()
    {
        return _notEmptyArray.GetEnumerator();
    }
}
Run Code Online (Sandbox Code Playgroud)

BenchmarkDotNet 在 .net framework 4.8 和 .net core 3.1 上报告了以下结果:

// * Summary *

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.329 (2004/?/20H1)
Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.1.301
  [Host]     : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT …
Run Code Online (Sandbox Code Playgroud)

c# arrays enumerator

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

如何在加载xml文件期间使用进度条

我想在加载远程xml文件期间显示进度条.我在Visual C#2008 Express Edition中使用Windows应用程序表单.

private void button1_Click(object sender, EventArgs e)
    {
        string text =  textBox1.Text;
        string url = "http://api.bing.net/xml.aspx?AppId=XXX&Query=" + text + "&Sources=Translation&Version=2.2&Market=en-us&Translation.SourceLanguage=en&Translation.TargetLanguage=De";

        XmlDocument xml = new XmlDocument();
        xml.Load(url);

        XmlNodeList node = xml.GetElementsByTagName("tra:TranslatedTerm");

        for (int x = 0; x < node.Count; x++ )
        {
            textBox2.Text = node[x].InnerText;
            progressbar1.Value = x;
        }
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码不能显示进度条加载..请建议我一些代码.提前致谢

c# progress-bar

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

泛型方法中的原始类型转换,无需装箱

在对我们的一个应用程序进行一些分析时,我发现了以下代码:

public TOut GetValue<TIn, TOut>(Func<TIn> getter)
{
    var value = getter();

    // Do some stuff with the value

    return (TOut)Convert.ChangeType(value, typeof(TOut));
}
Run Code Online (Sandbox Code Playgroud)

TInTOut可以是 int、double 或 string。

由于使用 int 或 double 时的装箱,这在分析会话中显示为堆分配的重要来源。的输入值Convert.ChangeType被装箱,因为该方法需要一个对象,并且返回值也出于同样的原因被装箱。

我正在尝试优化此代码,因为此方法用于高吞吐量服务,而这种分配是禁止的。理想情况下,我会将该方法重写为非通用方法,但该 API 被各​​个团队广泛使用,这种规模的重构将需要数月时间。与此同时,我正在尝试缓解这个问题,并找到一种在不更改 API 合约的情况下改善情况的方法。然而我已经为此苦苦挣扎了一段时间,尚未找到解决方案。

您是否知道一种方法,即使是丑陋的方法,在给定方法契约的情况下处理 int -> double 和 double -> int 转换而无需装箱?请注意,我无法更改参数,但我可以添加通用约束(例如where TIn : IConvertible,但这对我没有多大帮助)。

c# boxing

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

ToggleSwitch标题颜色

我有一个ToggleSwitch控件,标题文本是白色的.关闭和打开文本为黑色.如何将标题文本更改为黑色?代码如下

     <toolkit:ToggleSwitch x:Name="Toggle" Header="Background process" Margin="0,20,0,460" 
     Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Unchecked" Background="Black" 
     Foreground="black" FontSize="40">        
     </toolkit:ToggleSwitch>
Run Code Online (Sandbox Code Playgroud)

xaml windows-phone-7

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

读取页面导航上传递的字符串数组

当我从WP8.1中的另一个页面传递一个字符串时,我目前不确定如何读取参数.这实际上是我用来在导航到另一个页面时传递参数的代码:

String[] parameters = new String[3];
parameters[0] = ReliabilitySwitch.IsEnabled.ToString();
if (i != 2)
{
    parameters[1] = UnitsList.SelectedItem.ToString();
    parameters[2] = MethodSwitch.IsEnabled.ToString();
}
else
{
    parameters[1] = "2";
}    
Frame.Navigate(typeof(Nav),parameters);
Run Code Online (Sandbox Code Playgroud)

这说,我不知道如何从其他页面读取我传递的参数.我知道如何读取,例如,一个整数值.我试过像这样读这个参数,但我肯定在这个过程中遗漏了一些数据:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    String parameters;
    parameters = e.Parameter.ToString();
}
Run Code Online (Sandbox Code Playgroud)

c# navigation windows-runtime windows-store-apps windows-phone-8.1

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

为什么delgate类型的属性不起作用?

有人可以解释为什么这段代码不起作用?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void Something(string s);

        public class TestDelegate
        {
            public Something something
            {
                set
                {
                    Console.WriteLine("Registering delegate: {0}", something);
                    something = value;
                    Console.WriteLine("Delegate registered: {0}", something);
                }

                get
                {
                   Console.WriteLine("Get delegate");
                   return something;
                }
            }

            public void doSomething(string s)
            {
                something(s);
            }
        }

        static void Main(string[] args)
        {
            TestDelegate td = new TestDelegate();
            td.something = (string s) => Console.WriteLine(s);
            td.doSomething("test");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# delegates properties

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

在 UWP XAML(Windows 10)中未触发放置事件

我正在为 WINdows 10 开发一个应用程序,我想在两个列表中实现拖放结构。但 Drop 事件在 Windows 10 应用程序中未触发 .. 以前它在 Windows 8.1 中运行 .. 以下是我的代码:

<ListView Grid.Row="1" x:Name="TasksList" SelectionMode="None" HorizontalAlignment="Stretch" 
    ScrollViewer.VerticalScrollBarVisibility="Hidden" IsItemClickEnabled="True" 
    VerticalAlignment="Stretch" 
    ItemsSource="{Binding Tasks}"  ScrollViewer.VerticalScrollMode="Enabled" 
    CanReorderItems="True"  ShowsScrollingPlaceholders="False"
    DragItemsStarting="GridViewDragItemsStarting"  AllowDrop="True" IsSwipeEnabled="False"
    Drop="GridViewDrop" DragEnter="TasksList_DragEnter" CanDragItems="True"
    ItemContainerStyle="{StaticResource ClientListViewItemStyle}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,1" BorderBrush="{StaticResource MydesqBorderBrush}" Padding="10">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" Width="80" Height="60" Background="{Binding DueDateIndicatorColor,Converter={StaticResource HexToSolidColorBrushConverter}}" VerticalAlignment="Top" HorizontalAlignment="Center">
                        <Image x:Name="ImgClient" Source="{Binding Client.ClientPictureUrl,Converter={StaticResource ServerUrlConverter}}" Stretch="Fill" Visibility="{Binding Source, Converter={StaticResource NullToInvisibilityConverter}, ElementName=ImgClient}" Width="80" Height="60"/>
                        <Image x:Name="ImgAccount" Source="{Binding ImageUrl}" Width="35" Height="35" …
Run Code Online (Sandbox Code Playgroud)

xaml windows-10 uwp

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