当使用.Value时,我不确定为什么会收到'是在给定上下文中无效的方法'

问题描述 投票:0回答:1

在将字符串转换为数字时,我似乎无法使用.Value。

我正在使用它来使用Umbraco构建响应图像。

public static IHtmlString GetSrcSet(this IPublishedContent item, int width = 0, int height = 0, string widths = "300,600,768")
{
       StringBuilder sb = new StringBuilder(string.Empty);

       if (width == 0)
       {
           width = int.Parse(item.GetProperty("UmbracoWidth").ToString());
       }

       if (height == 0)
       {
           height = int.Parse(item.GetProperty("UmbracoHeight").ToString());
       }

       string[] splitWidths = widths.Split(',');

       foreach (string newWidth in splitWidths)
       {
           . . . 
       }

       return (new HtmlString(sb.ToString()));
}

我确信我需要在这些行上使用.Value

 if (width == 0)
 {
   width = int.Parse(item.GetProperty("UmbracoWidth").Value.ToString());
 }

 if (height == 0)
 {
   height = int.Parse(item.GetProperty("UmbracoHeight").Value.ToString());
 }

但是我得到

错误CS0119'PublishedElementExtensions.Value(IPublishedElement,string,string,string,Fallback,object)'是一种方法,在给定的上下文中无效

省略.Value

输入字符串的格式不正确。

c# umbraco umbraco8
1个回答
1
投票

您需要在“值”后面添加方括号。当您需要像方法一样调用它时,您当前正在调用Value就像它是一个属性。

 if (width == 0)
 {
   width = int.Parse(item.GetProperty("UmbracoWidth").Value().ToString());
 }

 if (height == 0)
 {
   height = int.Parse(item.GetProperty("UmbracoHeight").Value().ToString());
 }
© www.soinside.com 2019 - 2024. All rights reserved.