小编Tom*_*tom的帖子

BluetoothAdapter ActionDiscoveryFinished

我刚刚开始看看xamarin,现在我想扫描蓝牙设备.因此我使用以下代码:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
bluetoothAdapter.StartDiscovery();
Run Code Online (Sandbox Code Playgroud)

我得到以下class结果:

[BroadcastReceiver]
[IntentFilter(new [] {BluetoothAdapter.ActionDiscoveryFinished})]
public class BluetoothReceiver : BroadcastReceiver
{
    public BluetoothReceiver()
    {

    }

    public override void OnReceive(Context context, Intent intent)
    {
        if (BluetoothAdapter.ActionDiscoveryFinished.Equals(intent.Action))
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还将我的应用程序的权限设置为BLUETOOTHBLUETOOTH_ADMIN.一切正常,OnReceive-Method被正确调用.我现在的问题是:如何从OnReceive-Method的参数中获取找到的设备?

c# android bluetooth xamarin

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

C#将动作转换为动作<bool>

我有一个具有以下属性的类:

public Action<bool> Action { get; private set; }
Run Code Online (Sandbox Code Playgroud)

我有一个构造函数Action<bool> 作为参数.

现在我想添加另一个接受类型对象的构造函数Action.我怎样才能转换ActionAction<bool>?在这种情况下,bool参数应该为true.

c#

7
推荐指数
2
解决办法
3135
查看次数

数据绑定如果为false则启用

我在winforms-application中遇到数据绑定问题.在下面的代码中,我有一个数据绑定到文本框的enabled属性.启用状态取决于复选框的值.

tbAmount.DataBindings.Add("Enabled", checkBox, "Checked", 
                          false, DataSourceUpdateMode.OnPropertyChanged);
Run Code Online (Sandbox Code Playgroud)

如果选中该复选框,则在此代码中启用文本框.但我需要它倒置.如果取消选中该复选框,我希望启用文本框.我怎样才能实现这一目标?

c# data-binding

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

DeploymentItem-Attribut用于文件夹中的所有文件和文件夹

我想将所有文件部署在单元测试目录中的文件夹中.通过DeploymentItem-Attribut部署eacht项目太多了.

我试过类似的东西

[DeploymentItem(".\\")]
Run Code Online (Sandbox Code Playgroud)

要么

[DeploymentItem("*.*")]
Run Code Online (Sandbox Code Playgroud)

但两者都不起作用.

有没有人知道如何通过Deployment-Attribut部署所有子文件夹的所有文件?

c# unit-testing deploymentitem

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

具有MarkupExtension的IValueConverter

最近我读到了一个IValueConverter也继承自的东西MarkupExtension.它是这样的:

internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    private static BoolToVisibilityConverter converter;
    public BoolToVisibilityConverter()
    {
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
            {
                return Visibility.Visible;
            }
        }
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility)
        {
            Visibility visibility = (Visibility)value;
            if (visibility == Visibility.Collapsed)
            {
                return false;
            }
        }
        return true;
    }
    public override …
Run Code Online (Sandbox Code Playgroud)

c# wpf converter markup-extensions ivalueconverter

6
推荐指数
2
解决办法
2073
查看次数

检查窗口是否是MessageBox

我有一个功能,我可以对出现的窗口做出反应.现在我想知道出现的窗口是否是Messagebox.如果它是一个,我想阅读它的文本.

我已经能够提取Window-Title,Class-Name和Process-Id了

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能找到消息框的文本?

为了得到所有窗口我使用这个:

internal static class WindowFinder
    {
    private static readonly List<IntPtr> listWindows = new List<IntPtr>();

    private static bool IsWindowOrDialog(IntPtr hwnd, int lParam)
    {
        if (NativeMethods.IsHungAppWindow(hwnd) || !NativeMethods.IsWindowVisible(hwnd))
            return true;
        listWindows.Add(hwnd);
        return true;
    }

    internal static IEnumerable<IntPtr> GetAllWindows() …
Run Code Online (Sandbox Code Playgroud)

c# winapi interop

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

从Path加载程序集

我需要从某个位置而不是从GAC加载程序集.我无法从GAC中删除程序集,因为我的程序必须在不同的计算机上运行,​​我无法控制程序集是否在GAC中.

我在我的项目中添加了对DLL-File的引用.不幸的是,汇编是从GAC加载的.

App.xaml.csStartup-Event中尝试了以下代码

var directory = Thread.GetDomain().BaseDirectory;
var dllPath = Path.Combine(directory, "MyAssembly.dll");
var assembly = Assembly.LoadFrom(dllPath);
Run Code Online (Sandbox Code Playgroud)

不幸的是,程序集仍然是从GAC加载的.

即使程序集在GAC中,我还能从指定位置加载它?

c# gac

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

Check if Item in a DataGrid is already in view

I have a DataGrid where the ItemsSource is bound to an ObservableCollection<LogEntry>. On a click on a Button the user can scroll to a specific LogEntry. Therefor I use the following code:

private void BringSelectedItemIntoView(LogEntry logEntry)
{
    if (logEntry != null)
    {
        ContentDataGrid.ScrollIntoView(logEntry);
    }
}
Run Code Online (Sandbox Code Playgroud)

This just works fine. But what I don't like is: If the LogEntry already is in view then the DataGrid flickers shortly.

My question now is:

是否有可能检查DataGrid给定的 LogEntry 是否已经在视图中?

c# wpf datagrid

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

使用C#将数字签名应用于office-vba-macros

在互联网上搜索了一段时间后,没有成功,我会在这里询问。

互联网上的一些帖子说,不可能在Excel应用程序中将数字签名应用于VBA-Macro。但是我发现的所有文章都很老,所以我希望现在可以通过代码来完成。

我的目标是打开Excel文档,并假设存在宏,将数字签名应用于该文档的vba-macros。

我有以下代码来检测excel文档中是否存在VBMacros:

string filePath = @"E:\OfficeDocuments\Sample1.xlsm";
object isReadonly = true;
object missing = Missing.Value;
_Application application = new Microsoft.Office.Interop.Excel.Application();
workbook = application.Workbooks.Open(
                    filePath, missing, isReadonly, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing,
                    missing, missing, missing);
bool workbookHasVbProject = workbook.HasVBProject;
Run Code Online (Sandbox Code Playgroud)

那很好。

现在,我获取要用于签署宏的证书:

X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection x509Certificate2Collection = store.Certificates;
X509Certificate2Collection certificate2Collection = x509Certificate2Collection.Find(X509FindType.FindBySubjectName, "MyCertificate 01", true);
if (certificate2Collection.Count > 0)
{
   X509Certificate2 certificate = certificate2Collection[0];
}
Run Code Online (Sandbox Code Playgroud)

现在我不知道如何继续...

我试过了:

VBE vbe = application.VBE;
VBProject vbProject …
Run Code Online (Sandbox Code Playgroud)

c# excel digital-signature

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

UserControl 的相对源绑定

我创建了一个UserControl用于在我的应用程序中显示超链接。

这个标记UserControl看起来像:

<UserControl x:Class="MVVMExample.View.UserControls.ActionLink"
             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="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <TextBlock Margin="5">
                <Hyperlink Command="{Binding LinkCommand}" CommandParameter="{Binding LinkCommandParameter}">
                    <TextBlock Text="{Binding LinkText, UpdateSourceTrigger=PropertyChanged}"/>
                </Hyperlink>
        </TextBlock>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

这在代码隐藏中DataContextUserControl如下所示:

public partial class ActionLink : UserControl, INotifyPropertyChanged
{
    public static readonly DependencyProperty LinkTextProperty = DependencyProperty.Register(
        "LinkText", typeof (string), typeof (ActionLink), new PropertyMetadata(LinkTextChanged));

    public static readonly DependencyProperty LinkCommandParameterProperty = DependencyProperty.Register(
        "LinkCommandParameter", typeof (object), typeof (ActionLink),
        new PropertyMetadata(LinkCommandParameterChanged));

    public static readonly DependencyProperty LinkCommandProperty …
Run Code Online (Sandbox Code Playgroud)

c# wpf user-controls

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