首页 \ 问答 \ Image Binding,Silverlight,C#,WP7(Image Binding, Silverlight, C#, WP7)

Image Binding,Silverlight,C#,WP7(Image Binding, Silverlight, C#, WP7)

我已经成功地将存储在独立存储中的图像绑定到XAML中定义的列表框内的“图像”元素,没有任何问题。

我所做的是使用一个属性来存储图像的名称和位置在隔离存储中,并使用IValueConverter来实际打开文件并获取其流。

我很惊讶地看到我不能在一个更简单的设置中使用新的XAML。 我只有这个:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

这就是我在ModelView中所拥有的:

        public string PhotoName { get; set; }
        PhotoName = "Images\\0.jpg";

文件的路径和名称是正确的,因此它不是“找不到文件”的问题。

我的问题是我的IValueConverter的Convert方法永远不会被调用。

正如我之前提到的,我在另一个XAML(在同一个项目中)成功使用了这个转换器,一切正常。 唯一的区别是我绑定到ListBox使用的图像。

关于为什么我的IValueConverter的Convert方法在这种情况下没有被调用的任何想法或提示?

谢谢!

这是我尝试过的:

  1. 实现INotifyPropertyChanged

    public class PhotoNameClass : INotifyPropertyChanged
    {
    public string PhotoNameValue = string.Empty;
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    
    public string PhotoName
    {
        get
        {
            return this.PhotoNameValue;
        }
        set
        {
            if (value != this.PhotoNameValue)
            {
                this.PhotoNameValue = value;
                NotifyPropertyChanged("PhotoName");
            }
        }
    }
    
    }
    

这就是我分配给该物业的方式:

    public partial class ShowContent : PhoneApplicationPage
{
    PhotoNameClass photoClass = new PhotoNameClass();
}
   protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
      photoClass.PhotoName = "Images\\0.jpg";
      base.OnNavigatedTo(e);
    }
  1. 使用DependencyProperty

    public partial class ShowContent : PhoneApplicationPage
    

    {// PhotoNameClass photoClass = new PhotoNameClass();

    public string PhotoName
    {
        get { return (string)GetValue(PhotoNameProperty); }
        set { SetValue(PhotoNameProperty, value); }
    }
    
    public static readonly DependencyProperty PhotoNameProperty = DependencyProperty.Register("PhotoName", typeof(string), typeof(ShowContent), new PropertyMetadata("")); 
    
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       PhotoName = "Images\\0.jpg";
    }
    

以下是XAML的相关部分:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

其中,c定义为:

    xmlns:c="clr-namespace:ImageApplication"

谢谢。

UPDATE

我正在使用INotifyPropertyChanged实现来公开我的属性。

我已将XAML代码更改为:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:PhotoNameClass x:Key="photoClass" />
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
            <Image Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" DataContext="{StaticResource photoClass}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
            </Image>
    </Grid>
</Grid>

因此,我添加了image元素的“DataContext”属性,指定包装我的属性的类的名称。 在这种情况下, 调用IValueConverter的Convert方法 。 但是,这是不正确的,因为我在添加DataContext时在XAML中收到错误:“无法确定调用者的应用程序身份”,当然,进入Convert方法时,我的“value”字符串为空,因此,“右侧“”PhotoName“未填充。

谢谢你的任何想法......

PS这是我如何定义IValueConverter的Convert方法:

namespace ImageApplication
{
  public class BinaryToImageSRCConverter : IValueConverter
  { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    { // <-- Breakpoint here. Never gets triggered except after I added the "DataContext" attribute to the XAML image element.
      // but in that case, "value" is an empty string.
        if (value != null && value is string)
        {
               ... 

更新固定

我已经解决了这个问题。 我已将包含my property的类名分配给XAML代码中定义的映像的DataContext属性。

谢谢你们。 我希望这能帮助那里的人。


I have successfully managed to bind images stored in Isolated Storage to "Image" elements inside a listbox defined in XAML w/o any problems.

What I did was to use a property that stored the name and location of the image in isolated storage and an IValueConverter to actually open the file and get its stream.

I am surprised to see that I can't do this in a new XAML with an even easier setup. All I have is this:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

and this is what I have in the ModelView:

        public string PhotoName { get; set; }
        PhotoName = "Images\\0.jpg";

The path and name of the file is correct thus it is not a problem of "not finding the file".

My problem is that the Convert method of my IValueConverter never gets called.

As I mentioned before, I am successfully using this converter in another XAML (in the same project) and everything works correctly. The only difference is that I am binding to an image used by a ListBox.

Any ideas or hints as to why the Convert method of my IValueConverter is not called in this scenario ?

Thanks!

Here is what I have tried:

  1. Implementing INotifyPropertyChanged:

    public class PhotoNameClass : INotifyPropertyChanged
    {
    public string PhotoNameValue = string.Empty;
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    
    public string PhotoName
    {
        get
        {
            return this.PhotoNameValue;
        }
        set
        {
            if (value != this.PhotoNameValue)
            {
                this.PhotoNameValue = value;
                NotifyPropertyChanged("PhotoName");
            }
        }
    }
    
    }
    

This is how I assigned to the property:

    public partial class ShowContent : PhoneApplicationPage
{
    PhotoNameClass photoClass = new PhotoNameClass();
}
   protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
      photoClass.PhotoName = "Images\\0.jpg";
      base.OnNavigatedTo(e);
    }
  1. Using DependencyProperty

    public partial class ShowContent : PhoneApplicationPage
    

    { // PhotoNameClass photoClass = new PhotoNameClass();

    public string PhotoName
    {
        get { return (string)GetValue(PhotoNameProperty); }
        set { SetValue(PhotoNameProperty, value); }
    }
    
    public static readonly DependencyProperty PhotoNameProperty = DependencyProperty.Register("PhotoName", typeof(string), typeof(ShowContent), new PropertyMetadata("")); 
    
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       PhotoName = "Images\\0.jpg";
    }
    

Here are the relevant parts of the XAML:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

where, c is defined as:

    xmlns:c="clr-namespace:ImageApplication"

Thanks.

UPDATE

I am using the INotifyPropertyChanged implementation to expose my property.

I have changed the XAML code to this:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:PhotoNameClass x:Key="photoClass" />
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
            <Image Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" DataContext="{StaticResource photoClass}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
            </Image>
    </Grid>
</Grid>

Therefore, I added the "DataContext" attribute of the image element specifying the name of the class that wraps my property. In this scenario, the Convert method of the IValueConverter IS called. However, this is not right as I am getting an error in the XAML when adding DataContext: "Unable to determine application identity of the caller" and of course, stepping into the Convert method, my "value" string is empty therefore, the "right" "PhotoName" is not being populated.

Thanks for any ideas...

P.S. Here's how I defined my Convert method of the IValueConverter:

namespace ImageApplication
{
  public class BinaryToImageSRCConverter : IValueConverter
  { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    { // <-- Breakpoint here. Never gets triggered except after I added the "DataContext" attribute to the XAML image element.
      // but in that case, "value" is an empty string.
        if (value != null && value is string)
        {
               ... 

UPDATE FIXED

I have fixed the issue. I have assigned the class name wrapping my property to the DataContext property of the image defined in the XAML code.

Thank you all. I hope this will help somebody out there.


原文:https://stackoverflow.com/questions/7973473
更新时间:2023-12-10 16:12

最满意答案

你可能不得不荒谬地明白

excelWorkbook.Close (false, System.Reflection.Missing.Value,System.Reflection.Missing.Value) ;   
excelWorkbooks.Close();  
excelApp.Quit();  
Marshal.ReleaseComObject(excelWorksheet);  
Marshal.ReleaseComObject(excelSheets);  
Marshal.ReleaseComObject(excelWorkbooks);  
Marshal.ReleaseComObject(excelWorkbook);  
Marshal.ReleaseComObject(excelApp);  
excelWorksheet = null;  
excelSheets = null;  
excelWorkbooks = null;  
excelWorkbook = null;  
excelApp = null;  
GC.GetTotalMemory(false);  
GC.Collect();  
GC.WaitForPendingFinalizers();  
GC.Collect();  
GC.GetTotalMemory(true);  

我遇到过甚至没有这样做的情况。 我求助于搜索Excel进程并在其上调用Kill()。


You may have to go ridiculously explicit:

excelWorkbook.Close (false, System.Reflection.Missing.Value,System.Reflection.Missing.Value) ;   
excelWorkbooks.Close();  
excelApp.Quit();  
Marshal.ReleaseComObject(excelWorksheet);  
Marshal.ReleaseComObject(excelSheets);  
Marshal.ReleaseComObject(excelWorkbooks);  
Marshal.ReleaseComObject(excelWorkbook);  
Marshal.ReleaseComObject(excelApp);  
excelWorksheet = null;  
excelSheets = null;  
excelWorkbooks = null;  
excelWorkbook = null;  
excelApp = null;  
GC.GetTotalMemory(false);  
GC.Collect();  
GC.WaitForPendingFinalizers();  
GC.Collect();  
GC.GetTotalMemory(true);  

I've encountered situations where even that did not do it. I resorted to hunting down the Excel process and called Kill() on it.

相关问答

更多
  • 在MSDN上查看有关释放ComObjects的信息 System.Runtime.InteropServices.Marshal.ReleaseComObject( excel ); 如果你想在foreach循环中做其他事情,我建议用适当的代码块格式包装你的代码 foreach (var report in m_reports) { report.PrintReport(excel.Workbooks.Add().Sheets.Add()); } excel.Visible = true; ...
  • 尝试这个: excelBook.Close(0); excelApp.Quit(); 关闭工作簿时,您有三个可选参数: Workbook.close SaveChanges, filename, routeworkbook Workbook.Close(false)或者如果你正在做晚期绑定,有时使用零更容易使用Workbook.Close(0)这是我自动关闭工作簿时所做的。 我也去查找了它的文档,并在这里找到: Excel工作簿关闭 谢谢, Try this: excelBook.Close(0); ...
  • 不,这不是矫枉过正。 在这种情况下, pandas可以增加灵活性而不是限制。 例如,您可以允许用户上传以下任何内容:excel,csv,csv.gz。 由于pandas有一个处理各种格式的方法库,因此您可以编写一个函数来选择适当的方法并转换为数据帧。 一旦进入数据框,转换为字典是微不足道的。 这是我用于此目的的函数示例: import os, pandas as pd def read_file(filename, **kwargs): """Read file with **kwargs; f ...
  • 正如在评论中提到的那样,如果“表格”只是分散的单元簇,那真是一团糟。 你将不得不想出一些非常复杂的AI来尝试检测各种可能的情况。 如果不希望让客户改变他们的做法,但他们愿意调整一点点,我会建议使用真正的ExcelTables 。 有了这个,excel已经通过客户端或者您应用表格为您完成了工作。 例如,以下是工作表中的两个随机表格: 请注意,我只是复制/粘贴相同的单元格,但我然后通过右上角的按钮格式化为表格。 这样做不仅仅是让它看起来不错 - 它实际上创建了一个可以直接在EPPlus中引用的ExcelTabl ...
  • 你可以试试这个: function func1() { var p=window.open ("http://google.com", "pop", "width=300,height=150"); setTimeout (function(){p.close()},5000); } 窗口将显示5秒钟。 You can try this : function func1() { var p=window.open ("http://google.com", "pop", "width=300 ...
  • 设置ProcessStartInfo.UseShellExecute=false并再次检查。 As Hans Passant suggested, the "main" window of the process to be closed was indeed being guessed wrong, so calling Process.CloseMainWindow did not have the intended effect.
  • @_teresko - 谢谢。 我的问题是我对MVC的错误理解和实现。 我已经进一步阅读,特别是在这里 。 你的评论迫使我重新审视我的申请流程,我发现了这个问题。 瘦控制器有时不是最佳解决方案。 @_teresko - Thank you. My issue is with my incorrect understanding and implementation of MVC. I have read further, and particularly here. Your comments have f ...
  • 你可能不得不荒谬地明白 : excelWorkbook.Close (false, System.Reflection.Missing.Value,System.Reflection.Missing.Value) ; excelWorkbooks.Close(); excelApp.Quit(); Marshal.ReleaseComObject(excelWorksheet); Marshal.ReleaseComObject(excelSheets); Marshal.Releas ...
  • 控制用户的决定。 这是简单的代码,如果需要可以改进。 Private Sub Workbook_BeforeClose(Cancel As Boolean) 'take control of what user can do: If MsgBox("Do you want to save and exit?", vbYesNo) = vbYes Then Application.DisplayAlerts = False ThisWorkbook.Save 'call ...
  • 首先,假设您的CSV文件名为data.csv ,是制表符分隔的,并包含: A0001 SVC001 N032 TSC538 TS_BLAH A0002 SVC002 N384 TSC002 TS_BLAH A0006 SVC1223 N456 TSC002 TN_foo A0006 SVC1223 N456 TSC004 ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的