zoukankan      html  css  js  c++  java
  • WPF 调试时拖拽不生效

    WPF窗体代码

    <Window x:Class="SerialLabelDemo.Test.Window10"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:SerialLabelDemo.Test"
            mc:Ignorable="d" AllowDrop="True"
                Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="35"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel Grid.Row="0" Orientation="Horizontal" Background="#CC0088FF">
                <Button
              Name="btClear" Click="ClickClear" Content="Clear" Margin="5"
            />
                <Border BorderBrush="Black" BorderThickness="1" MaxHeight="25">
                    <CheckBox
                Name="cbWrap"
                Content="Wrap Content" 
                IsChecked="False" 
                Margin="5" Padding="5,0,0,0" 
                VerticalAlignment="Center" VerticalContentAlignment="Center" 
                Click="ClickWrap"
              />
                </Border>
                <Label 
              Name="lblInstructions" 
              HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
              FontWeight="Bold"
              Content="     Drop a single file below to display its contents.     "
            />
            </StackPanel>
    
            <TextBox
            Name="tbDisplayFileContents" 
            Grid.Row="1" 
            AcceptsReturn="True" AcceptsTab="True" 
            AllowDrop="True" 
            BorderThickness="1" BorderBrush="Black" 
            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
            
            PreviewDragOver="EhDragOver" PreviewDrop="EhDrop"/>
            <TextBlock Text="管理员运行是不可以进行拖拽的"/>
    
        </Grid>
    </Window>

    窗体后台代码

        public partial class Window10 : Window
        {
            public Window10()
            {
                InitializeComponent();
    
                if ((bool)cbWrap.IsChecked)
                    tbDisplayFileContents.TextWrapping = TextWrapping.Wrap;
                else
                    tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap;
            }
    
            private void ClickClear(object sender, RoutedEventArgs args)
            {
                tbDisplayFileContents.Clear();
            }
    
            private void ClickWrap(object sender, RoutedEventArgs args)
            {
                if ((bool)cbWrap.IsChecked)
                    tbDisplayFileContents.TextWrapping = TextWrapping.Wrap;
                else
                    tbDisplayFileContents.TextWrapping = TextWrapping.NoWrap;
            }
    
            private void EhDragOver(object sender, DragEventArgs args)
            {
                // As an arbitrary design decision, we only want to deal with a single file.
                args.Effects = IsSingleFile(args) != null ? DragDropEffects.Copy : DragDropEffects.None;
    
                // Mark the event as handled, so TextBox's native DragOver handler is not called.
                args.Handled = true;
            }
    
            private void EhDrop(object sender, DragEventArgs args)
            {
                // Mark the event as handled, so TextBox's native Drop handler is not called.
                args.Handled = true;
    
                var fileName = IsSingleFile(args);
                if (fileName == null) return;
    
                var fileToLoad = new StreamReader(fileName);
                tbDisplayFileContents.Text = fileToLoad.ReadToEnd();
                fileToLoad.Close();
    
                // Set the window title to the loaded file.
                Title = "File Loaded: " + fileName;
            }
    
            // If the data object in args is a single file, this method will return the filename.
            // Otherwise, it returns null.
            private string IsSingleFile(DragEventArgs args)
            {
                // Check for files in the hovering data object.
                if (args.Data.GetDataPresent(DataFormats.FileDrop, true))
                {
                    var fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[];
                    // Check fo a single file or folder.
                    if (fileNames.Length == 1)
                    {
                        // Check for a file (a directory will return false).
                        if (File.Exists(fileNames[0]))
                        {
                            // At this point we know there is a single file.
                            return fileNames[0];
                        }
                    }
                }
                return null;
            }
        }
    

      运行画面:

     调试时发现无法拖拽:

    原因,以管理员启动VS进行调试是不能拖拽外部文件到程序中的

    在debug目录下以管理员运行exe运行也是一样的结果

    说是UAC检测的系统上管理员权限开启的程序时无法支持外部拖拽的

    同行描述原因见https://www.cnblogs.com/swack/p/10508649.html

  • 相关阅读:
    Mac电脑maven安装与配置
    解决Mac OS X中IDEA启动慢以及debug卡死问题
    如何在Mac上启用root用户或更改root密码
    mac文本编辑器软件,五大适用于Mac修订的文本编辑器,nodepad++替代软件
    mac系统到10.14以上,navicat无法打开,一直显示已损坏解决办法
    mac苹果电脑AppleID注册或者登录appstore时提示:您没有完整填写表格,请输入您的出生年份的解决方法
    mac苹果电脑使用相关,开发环境配置指南(持续更新)
    bitmap to base64
    Multiple actions were found that match the request in Web Api
    vue get attribute value
  • 原文地址:https://www.cnblogs.com/wandia/p/13571061.html
Copyright © 2011-2022 走看看